﻿marriage_interaction_on_accept_effect = {
	#Use hook
	if = {
		limit = { 
			exists = scope:hook
			always = scope:hook 
		}
		scope:actor = {
			use_hook = scope:recipient
		}
		#To make sure betrothals can't be broken if a hook was used
		if = {
			limit = {
				OR = {
					scope:secondary_actor = { is_adult = no }
					scope:secondary_recipient = { is_adult = no }
				}
			}
			scope:secondary_actor = {
				set_variable = {
					name = hook_used_for_betrothal
					value = scope:secondary_recipient
				}
			}
		}
	}
	
	#Penalties for marrying a lowborn
	scope:actor = {
		if = {
			limit = {
				this = scope:secondary_actor # We check that you're marrying them yourself
				scope:secondary_recipient = { is_lowborn = yes }
			}
			# Vassal dispositions get aggy.
			## Actual effect applied in the on_marriage on_action.
			show_as_tooltip = {
				vassals_dislike_marry_lowborn_effect = {
					SPOUSE = scope:secondary_recipient
					RULER = scope:actor
				}
			}
			#Legitimacy loss for marrying a lowborn
			if = {
				limit = {
					is_valid_for_legitimacy_change = yes
					NOT = { has_variable = lowborn_legitimacy_cooldown }
				}
				if = {
					limit = { is_ai = no }
					if = { #Marrying a lowborn PS will always decrease legitimacy
						limit = {
							OR = {
								NOT = { exists = primary_spouse }
								primary_spouse ?= scope:secondary_recipient
							}
						}
						send_interface_toast = {
							type = event_toast_effect_good
							title = married_lowborn_toast
							left_icon = scope:actor
							add_legitimacy = {
								value = medium_legitimacy_loss
								multiply = scope:actor.primary_title.tier
							}
						}
					}
					else_if = { #Else, only non-tribal, non-nomad rulers will lose legitimacy for marrying a lowborn secondary spouse
						limit = {
							primary_spouse ?= { 
								NOT = { 
									this = scope:secondary_recipient 
								}
							}
							NOR = {
								government_has_flag = government_is_tribal
								government_has_flag = government_is_nomadic
							}
						}
						send_interface_toast = {
							type = event_toast_effect_good
							title = married_lowborn_toast
							left_icon = scope:actor
							add_legitimacy = {
								value = -5
								multiply = scope:actor.primary_title.tier
							}
						}
					}
				}
				else = { # To avoid destroying polygamy AIs
					send_interface_toast = {
						type = event_toast_effect_good
						title = married_lowborn_toast
						left_icon = scope:actor
						if = { #Marrying a lowborn PS will always decrease legitimacy
							limit = {
								OR = {
									NOT = { exists = primary_spouse }
									primary_spouse ?= scope:secondary_recipient
								}
							}
							add_legitimacy = { value = minor_legitimacy_loss }
						}
					}
					hidden_effect = {
						set_variable = {
							name = lowborn_legitimacy_cooldown
							years = 10
						}
					}
				}
			}
		}
		if = {
			limit = {
				this = scope:secondary_recipient # We check that you're marrying them yourself
				scope:secondary_actor = { is_lowborn = yes }
			}
			# Vassal dispositions get aggy.
			## Actual effect applied in the on_marriage on_action.
			show_as_tooltip = {
				vassals_dislike_marry_lowborn_effect = {
					SPOUSE = scope:secondary_actor
					RULER = scope:actor
				}
			}
			#Legitimacy loss for marrying a lowborn
			if = {
				limit = {
					is_valid_for_legitimacy_change = yes
					NOT = { has_variable = lowborn_legitimacy_cooldown }
				}
				if = {
					limit = { is_ai = no }
					if = { #Marrying a lowborn PS will always decrease legitimacy
						limit = {
							OR = {
								NOT = { exists = primary_spouse }
								primary_spouse ?= scope:secondary_actor
							}
						}
						send_interface_toast = {
							type = event_toast_effect_good
							title = married_lowborn_toast
							left_icon = scope:actor
							add_legitimacy = {
								value = medium_legitimacy_loss
								multiply = scope:actor.primary_title.tier
							}
						}
					}
					else_if = { #Else, only non-tribal, non-nomad rulers will lose legitimacy for marrying a lowborn secondary spouse
						limit = {
							primary_spouse ?= { 
								NOT = { 
									this = scope:secondary_actor 
								}
							}
							NOR = {
								government_has_flag = government_is_tribal
								government_has_flag = government_is_nomadic
							}
						}
						send_interface_toast = {
							type = event_toast_effect_good
							title = married_lowborn_toast
							left_icon = scope:actor
							add_legitimacy = {
								value = -5
								multiply = scope:actor.primary_title.tier
							}
						}
					}
				}
				else = { # To avoid destroying polygamy AIs
					send_interface_toast = {
						type = event_toast_effect_good
						title = married_lowborn_toast
						left_icon = scope:actor
						if = { #Marrying a lowborn PS will always decrease legitimacy
							limit = {
								OR = {
									NOT = { exists = primary_spouse }
									primary_spouse ?= scope:secondary_actor
								}
							}
							add_legitimacy = { value = minor_legitimacy_loss }
						}
					}
					hidden_effect = {
						set_variable = {
							name = lowborn_legitimacy_cooldown
							years = 10
						}
					}
				}
			}
		}
		else = { # If they're not a lowborn AND you and the recipient are both Nomad we may give you some Legitimacy
			if = {
				limit = {
					is_valid_for_nomadic_legitimacy_change = yes
					scope:recipient = { government_has_flag = government_is_nomadic }
					mpo_lower_nomad_authority_trigger = { CHARACTER = scope:recipient } # Only if recipient has higher Dominance than you
					#They were not previously consorts
					scope:secondary_actor = {
						NOR = {
							any_former_spouse = {
								this = scope:secondary_recipient
							}
							any_former_concubinist = {
								this = scope:secondary_recipient
							}
						}
					}
					scope:secondary_recipient = {
						NOR = {
							any_former_spouse = {
								this = scope:secondary_actor
							}
							any_former_concubinist = {
								this = scope:secondary_actor
							}
						}
					}
				}
				add_legitimacy = medium_legitimacy_gain
			}
			
			# Check for legitimacy gain from marrying into a higher rank
			if = {
				limit = {
					scope:actor = { this = scope:secondary_actor }
				}
				marriage_legitimacy_rank_effect = {
					RULER = scope:secondary_actor
					OTHER = scope:secondary_recipient
				}
			}
			else_if = {
				limit = {
					scope:actor = { this = scope:secondary_recipient }
				}
				marriage_legitimacy_rank_effect = {
					RULER = scope:secondary_recipient
					OTHER = scope:secondary_actor
				}
			}
		}
	}
	
	#No longer Herder
	if = {
		limit = {
			scope:secondary_recipient = {
				is_landed = yes
				government_has_flag = government_is_herder
			}
		}
		scope:actor = {
			switch = {
				trigger = has_government
				feudal_government = {
					scope:secondary_recipient = { change_government = feudal_government }
				}
				tribal_government = {
					scope:secondary_recipient = { change_government = tribal_government }
				}
				clan_government = {
					scope:secondary_recipient = { change_government = clan_government }
				}
				nomad_government = {
					scope:secondary_recipient = { change_government = nomad_government }
				}
				administrative_government = {
					scope:secondary_recipient = { change_government = administrative_government }
				}
				landless_adventurer_government = {
					scope:secondary_recipient = { change_government = landless_adventurer_government }
				}
			}
		}
	}

	#No longer concubine
	if = {
		limit = {
			scope:secondary_recipient = { is_concubine = yes }
		}
		scope:secondary_recipient = {
			random_consort = {
				limit = {
					any_concubine = { this = scope:secondary_recipient }
				}
				remove_concubine = scope:secondary_recipient
			}
		}
	}
	if = {
		limit = {
			scope:secondary_actor = { is_concubine = yes }
		}
		scope:secondary_actor = {
			random_consort = {
				limit = {
					any_concubine = { this = scope:secondary_actor }
				}
				remove_concubine = scope:secondary_actor
			}
		}
	}
	# A Grand Wedding was promised
	if = {
		limit = {
			exists = scope:grand_wedding_promise
			scope:grand_wedding_promise = yes
		}
		set_grand_wedding_betrothal_variables = {
			SPOUSE_1 = scope:secondary_actor
			SPOUSE_2 = scope:secondary_recipient
			HOST = scope:actor
			PROMISEE = scope:recipient
		}
	}

	#Marriage notification events
	hidden_effect = {
		scope:actor = {
			if = {
				limit = { NOT = { this = scope:recipient } }
				trigger_event = marriage_interaction.0010
			}
			else = { #In my own court
				if = {
					limit = { #Betrothal?
						OR = {
							scope:secondary_actor = { is_adult = no }
							scope:secondary_recipient = { is_adult = no }
							scope:secondary_actor = { has_been_promised_grand_wedding = yes }
							scope:secondary_recipient = { has_been_promised_grand_wedding = yes }
						}
					}
					send_interface_toast = {
						type = event_toast_effect_good
						title = arrange_marriage_interaction_accept_betrothal_toast
						left_icon = scope:secondary_actor
						right_icon = scope:secondary_recipient
						custom_tooltip  = arrange_marriage_interaction_accept_betrothal_toast_desc
					}
				}
				else = {
					send_interface_toast = {
						type = event_toast_effect_good
						title = arrange_marriage_interaction_accept_toast
						left_icon = scope:secondary_actor
						right_icon = scope:secondary_recipient
						custom_tooltip  = arrange_marriage_interaction_accept_toast_desc
					}
				}
			}
		}

		# Apply any relevant opinion penalties for relatives of spouse.
		scope:secondary_actor = {
			if = { # Polygamy
				limit = {
					any_consort = {
						count >= 2
					}
				}
				every_consort = {
					# If the spouse themselves does not believe in polygamy...
					limit = {
						hp_accepts_harems = no
					}

					#... then family members who also do not believe in polygamy aren't happy about having a relative forced into polygamous union with you.
					every_close_family_member = {
						if = {
							limit = { 
								hp_accepts_harems = no
							}
							add_opinion = {
								target = scope:secondary_actor
								modifier = relative_in_blasphemous_union_opinion
							}
						}
					}

					# NOTE: Spouse's own opinions handled in the 'on_marriage' code on_action inside of marriage_concubinage.txt, and are not included here.
				}
			}
		}
		scope:secondary_recipient = {
			if = { #Same-sex relations
				limit = {
					allowed_to_marry_same_sex_trigger = no
					sex_same_as = scope:secondary_actor
				}
				every_close_family_member = {
					if = {
						limit = { 
							allowed_to_marry_same_sex_trigger = no
						}
						add_opinion = {
							target = scope:secondary_actor
							modifier = relative_in_blasphemous_union_opinion
						}
					}
				}
			}
		}
		# Courtly vassals like grand weddings
		scope:secondary_actor = {
			if = {
				limit = {
					any_vassal = {
						has_vassal_stance = courtly
					}
				}
				if = {
					limit = {
						has_been_promised_grand_wedding = yes
					}
					every_vassal = {
						limit = {
							has_vassal_stance = courtly
						}
						add_opinion = {
							target = scope:secondary_actor
							modifier = prestigious_wedding_opinion
						}
					}
				}
			}
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					any_vassal = {
						has_vassal_stance = courtly
					}
				}
				if = {
					limit = {
						has_been_promised_grand_wedding = yes
					}
					every_vassal = {
						limit = {
							has_vassal_stance = courtly
						}
						add_opinion = {
							target = scope:secondary_recipient
							modifier = prestigious_wedding_opinion
						}
					}
				}
			}
		}
	}

	###MANAGEMENT SCRIPT FOR VARIOUS EVENTS/CONTENT###
	
	#If your spouse councillor had boosted the chance of marriage, remove modifier
	if = {
		limit = {
			scope:secondary_actor = {
				has_character_modifier = heir_easier_to_marry_off_modifier
			}
			scope:actor = { player_heir = scope:secondary_actor }
		}
		scope:secondary_actor = {
			remove_character_modifier = heir_easier_to_marry_off_modifier
		}
	}
	if = {
		limit = {
			scope:secondary_actor = {
				is_child_of = scope:actor
				has_character_modifier = child_easier_to_marry_off_modifier
			}
		}
		scope:secondary_actor = {
			remove_character_modifier = child_easier_to_marry_off_modifier
		}
	}

	#AI Break up consideration after marriage
	scope:secondary_recipient = { 
		if = {
			limit = {
				is_adult = yes
				is_ai = yes
			}
			scope:secondary_actor = { save_scope_as = new_spouse_secondary_actor }
			trigger_event = lover.0205
		}
	}
	scope:secondary_actor = {
		if = {
			limit = {
				is_adult = yes
				is_ai = yes
			}
			scope:secondary_recipient = { save_scope_as = new_spouse_secondary_recipient }
			trigger_event = lover.0205
		}
	}

	# Struggle parameters

	#interfaith wedding costs piety
	if = {
		limit = {
			scope:secondary_actor = {
				NOT = { faith = scope:secondary_recipient.faith }
			}
			scope:actor ={
				any_character_struggle = {
					has_struggle_phase_parameter = interfaith_marriages_between_involved_characters_costs_piety
				}
			}
		}
		scope:actor = {
			add_piety = major_piety_loss
		}
	}
	#interfaith wedding provides piety
	if = {
		limit = {
			scope:secondary_actor = {
				NOT = { faith = scope:secondary_recipient.faith }
			}
			scope:actor ={
				any_character_struggle = {
					has_struggle_phase_parameter = interfaith_marriages_between_involved_characters_gives_piety
				}
			}
		}
		scope:actor = {
			add_piety = medium_piety_gain
		}
	}

	#other stuff 
	#Khurramites and Mazdakists gain piety from marrying lowborn
	if = {
		limit = {
			scope:secondary_recipient = {
				is_lowborn = yes 
			}
			scope:actor.faith = {
				has_doctrine = tenet_communal_possessions 
			}
		}
		scope:actor = {
			add_piety = medium_piety_gain
		}
	}
	calyver_marriage_harem_politics_effect = yes
}

calyver_marriage_harem_politics_effect = {
	#Concubines get jealous
	if = {
		limit = {
			scope:secondary_recipient = {
				any_concubine = {
					count >= 1
				}
			}
		}
		hidden_effect = {
			scope:secondary_recipient = {
				every_concubine = {
					calyver_update_active_consort_opinion_effect = { PARTNER = scope:secondary_recipient SPOUSE = scope:secondary_actor }
				}
			}
		}
	}
	if = {
		limit = {
			scope:secondary_actor = {
				any_concubine = {
					count >= 1
				}
			}
		}
		hidden_effect = {
			scope:secondary_actor = {
				every_concubine = {
					calyver_update_active_consort_opinion_effect = { PARTNER = scope:secondary_actor SPOUSE = scope:secondary_recipient }
				}
			}
		}
	}
	
	#Promotions and demotions
	if = {
		limit = {
			scope:secondary_recipient = {
				has_demoted_or_divorced_opinion_trigger = {
					TARGET = scope:secondary_actor
				}
			}
		}
		spouse_management_effect = {
			CONSORT = scope:secondary_recipient
			PARTNER = scope:secondary_actor
		}
	}
	if = {
		limit = {
			scope:secondary_actor = {
				has_demoted_or_divorced_opinion_trigger = {
					TARGET = scope:secondary_recipient
				}
			}
		}
		spouse_management_effect = {
			CONSORT = scope:secondary_actor
			PARTNER = scope:secondary_recipient
		}
	}
	else_if = {
		limit = {
			exists = scope:recipient
			NOT = {
				scope:actor = scope:recipient
			}
		}
		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_gain
			DESC = clan_unity_marry_off.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

	#New consorts
	if = {
		limit = {
			scope:secondary_actor = {
				NOR = {
					any_former_spouse = {
						this = scope:secondary_recipient
					}
					any_former_concubinist = {
						this = scope:secondary_recipient
					}
				}
			}
			scope:secondary_recipient = {
				NOR = {
					any_former_spouse = {
						this = scope:secondary_actor
					}
					any_former_concubinist = {
						this = scope:secondary_actor
					}
				}
			}
		}
		#New consorts
		scope:secondary_recipient = {
			if = {
				limit = {
					is_landed = no
				}
				add_character_flag = {
					flag = flag_new_consort
					years = 2
				}
			}
			if = {
				limit = {
					has_variable = favor
				}
				remove_variable = favor
			}
		}
		scope:secondary_actor = {
			if = {
				limit = {
					is_landed = no
				}
				add_character_flag = {
					flag = flag_new_consort
					years = 2
				}
			}
			if = {
				limit = {
					has_variable = favor
				}
				remove_variable = favor
			}
		}
	}
	else = {
		scope:secondary_actor = {
			if = {
				limit = {
					any_former_concubinist = {
						this = scope:secondary_recipient
					}
					is_landed = no
					NOT = {
						has_demoted_or_divorced_opinion_trigger = {
							TARGET = scope:secondary_recipient
						}
					}
				}
				#Promoted consorts
				if = {
					limit = { has_character_flag = flag_scolded_consort }
					remove_character_flag = flag_scolded_consort
				}
				if = {
					limit = {
						exists = scope:secondary_recipient.primary_spouse
						this = scope:secondary_recipient.primary_spouse
					}
					calyver_favor_big_gain_effect = yes
				}
				else = {
					calyver_favor_moderate_gain_effect = yes
				}
				hidden_effect = {
					every_close_or_extended_family_member = {
						limit = {
							NOR = {
								this = scope:secondary_recipient
								this = scope:secondary_actor
							}
						}
						send_interface_message = {
							type = event_generic_good_text 
							title = grand_consort_promotion_t 
							right_icon = scope:secondary_actor
							left_icon = scope:secondary_recipient
							show_as_tooltip = {
								scope:secondary_recipient = {
									marry = scope:secondary_actor
								}
							}
						}
						if = {
							limit = {
								NOR = {
									has_opinion_modifier = {
										target = scope:secondary_recipient
										modifier = forced_family_concubine_marriage_opinion
									}
									has_opinion_modifier = {
										target = scope:secondary_recipient
										modifier = thankful_consort_relative_opinion
									}
								}
								hp_accepts_harems = yes
							}
							add_opinion = {
								target = scope:secondary_recipient
								modifier = thankful_consort_relative_opinion
							}
						}
					}
				}
			}
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					any_former_concubinist = {
						this = scope:secondary_actor
					}
					is_landed = no
					NOT = {
						has_demoted_or_divorced_opinion_trigger = {
							TARGET = scope:secondary_actor
						}
					}
				}
				#Promoted consorts
				if = {
					limit = { has_character_flag = flag_scolded_consort }
					remove_character_flag = flag_scolded_consort
				}
				if = {
					limit = {
						exists = scope:secondary_actor.primary_spouse
						this = scope:secondary_actor.primary_spouse
					}
					calyver_favor_big_gain_effect = yes
				}
				else = {
					calyver_favor_moderate_gain_effect = yes
				}
				hidden_effect = {
					every_close_or_extended_family_member = {
						limit = {
							NOR = {
								this = scope:secondary_actor
								this = scope:secondary_recipient
							}
						}
						send_interface_message = {
							type = event_generic_good_text 
							title = grand_consort_promotion_t 
							right_icon = scope:secondary_recipient
							left_icon = scope:secondary_actor
							show_as_tooltip = {
								scope:secondary_actor = {
									marry = scope:secondary_recipient
								}
							}
						}
						if = {
							limit = {
								NOR = {
									has_opinion_modifier = {
										target = scope:secondary_actor
										modifier = forced_family_concubine_marriage_opinion
									}
									has_opinion_modifier = {
										target = scope:secondary_actor
										modifier = thankful_consort_relative_opinion
									}
								}
								hp_accepts_harems = yes
							}
							add_opinion = {
								target = scope:secondary_actor
								modifier = thankful_consort_relative_opinion
							}
						}
					}
				}
			}
		}
	}

	# No longer eligible for concubinage for Muslim rulers
	if = {
		limit = {
			scope:secondary_actor = {
				has_character_flag = abducted_non_believer
			}
			scope:secondary_recipient = {
				religion = religion:islam_religion
				trigger_if = {
					limit = {
						has_character_flag = abducted_non_believer
					}
					is_ruler = yes
				}
				trigger_else = {
					NOT = {
						has_character_flag = abducted_non_believer
					}
				}
			}
		}
		scope:secondary_actor = {
			remove_character_flag = abducted_non_believer
		}
	}
	if = {
		limit = {
			scope:secondary_recipient = {
				has_character_flag = abducted_non_believer
			}
			scope:secondary_actor = {
				religion = religion:islam_religion
				trigger_if = {
					limit = {
						has_character_flag = abducted_non_believer
					}
					is_ruler = yes
				}
				trigger_else = {
					NOT = {
						has_character_flag = abducted_non_believer
					}
				}
			}
		}
		scope:secondary_recipient = {
			remove_character_flag = abducted_non_believer
		}
	}

	child_promotion_effect = {
		PARENT_1 = scope:secondary_actor
		PARENT_2 = scope:secondary_recipient
	}
}

concubine_on_accept_effect = {
	scope:recipient = {
		if = {
			limit = {
				religion = religion:islam_religion
				NOT = {
					has_character_flag = abducted_non_believer
				}
				is_imprisoned = no
				scope:secondary_actor.religion = religion:islam_religion
			}
			scope:secondary_actor = {
				add_piety = medium_piety_loss
			}
		}	
		trigger_event = marriage_interaction.0040
		if = {
			# Victim is angry for being forced into concubinage.
			limit = {
				OR = {
					has_trait = celibate
					is_imprisoned = yes
				}
			}
			hidden_effect = {
				if = {
					limit = {
						any_consort = {
							NOT = { this = scope:actor }
							count >= 1
						}
					}
					every_consort = {
						limit = {
							NOT = { this = scope:actor }
						}
						scope:recipient = {
							former_consort_forced_concubine_effect = {
								PARTNER = prev
								CONSORT = scope:recipient
							}
						}
					}
				}
			}
			if = {
				limit = {
					is_imprisoned = yes
				}
				release_from_prison = yes
			}
			add_opinion = {
				target = scope:actor
				modifier = forced_me_concubine_marriage_opinion
			}

			# Family members of victim are also angry that you've forced them into concubinage.
			every_close_family_member = {
				limit = { 
					NOT = { this = scope:actor } # Don't hate yourself.
				}
				custom = all_family_members
				add_opinion = {
					target = scope:actor
					modifier = forced_family_concubine_marriage_opinion
				}
				hidden_effect = {
					send_interface_message = {
						type = msg_family_became_concubine
						title = msg_close_family_member_taken_as_concubine_title
						right_icon = scope:recipient
						left_icon = scope:secondary_actor
						show_as_tooltip = { scope:secondary_actor = { make_concubine = scope:recipient } }
					}
				}

				# Family members are even *more* angry if they don't believe in concubines/polyamory (different from polygamy). Also if they don't accept same-sex relationships
				if = {
					limit = { 
						OR = {
							NOT = { hp_accepts_concubinage = yes }
							NOT = { faith = { has_doctrine = tenet_polyamory } }
							AND = {
								allowed_to_marry_same_sex_trigger = no
								scope:recipient = { sex_same_as = scope:secondary_actor }
							}
						}
					}
					add_opinion = {
						target = scope:actor
						modifier = relative_in_blasphemous_union_opinion
					}
				}
			}	

			# Not up to them anymore...
			if = {
				limit = { has_trait = celibate }
				remove_trait = celibate
			}			
					
			# Spouses, if any exist, are also furious.
			if = {
				limit = {
					is_married = yes
					#A check to make sure you haven't somehow married them during the consideration time
					NOT = { any_spouse = { this = scope:actor } }
				}
				every_spouse = {
					hidden_effect = {
						if = {
							limit = {
								primary_spouse ?= {
									this = scope:recipient
								}
								any_spouse = {
									count >= 2
								}
							}
							add_character_flag = {
								flag = flag_lowborn_exception
								days = 2
							}
							scope:recipient = {
								new_primary_spouse_divorce_effect = {
									PREVIOUS_PS = scope:recipient
									PARTNER = prev
								}
							}
							remove_character_flag = flag_lowborn_exception
						}
					}
					show_as_tooltip = {
						add_opinion = {
							target = scope:actor
							modifier = forced_spouse_concubine_marriage_opinion
						}
						divorce = scope:recipient # no additional opinion hit, no scripted effect
					}
					trigger_event = marriage_interaction.0041
				}
			}
					
			# Stealing concubines pisses the former concubinist off.
			if = {
				limit = {
					is_concubine = yes
					#A check to make sure you haven't somehow gotten them as concubine during the consideration time
					NOT = { any_consort = { this = scope:actor } }
				}
				this.concubinist = {
					show_as_tooltip = {
						add_opinion = {
							target = scope:secondary_actor
							modifier = stole_concubine_opinion
						}
						remove_concubine = scope:recipient # no additional opinion hit, no scripted effect
					}
					trigger_event = marriage_interaction.0041
				}
			}

			# Break any betrothals which may exist (without additional opinion penalties).
			if = {
				limit = {
					exists = betrothed
					#A check to make sure you haven't somehow gotten betrothed to them during the consideration time
					NOT = { betrothed = scope:actor }
				}
				betrothed = { trigger_event = marriage_interaction.0041 }
				break_betrothal = betrothed
			}

			# Having concubines means they're now no longer yours.
			if = {
				limit = { number_of_concubines > 0	}
				every_concubine = {
					scope:recipient = {
						remove_concubine = prev
					}
				}
			}
			clear_demoted_consort_divorce_modifiers_effect = yes
		}
		make_concubine_harem_politics_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
		}
	}

	# Send us a notification about what we just did.
	scope:secondary_actor = {
		hidden_effect = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = msg_concubine_taken_title
				right_icon = scope:recipient
				if = {
					limit = {
						scope:recipient = {
							NOT = {
								is_concubine_of = scope:secondary_actor
							}
						}
					}
					make_concubine = scope:recipient
				}
			}
		}
	}
}

make_concubine_harem_politics_effect = {
	# First save the scopes with specific names for House Unity
	$CHARACTER$ = { save_scope_as = harem_actor } #The actor
	$TARGET$ = { save_scope_as = harem_recipient } #The concubine

	if = {
		limit = {
			has_demoted_or_divorced_opinion_trigger = {
				TARGET = $CHARACTER$
			}
		}
		spouse_demotion_effect = {
			ACTOR = $CHARACTER$
			RECIPIENT = $TARGET$
		}
	}
	else = {
		if = {
			limit = {
				is_landed = no
			}
			add_character_flag = {
				flag = flag_new_consort
				years = 2
			}
		}
		if = {
			limit = {
				has_variable = favor
			}
			remove_variable = favor
		}
		
		# Now we can use the saved scopes for House Unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:harem_actor
			TARGET = scope:harem_recipient
			VALUE = minor_unity_gain
			DESC = clan_unity_concubinage.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
		child_demotion_effect = {
			PARENT_1 = $CHARACTER$
			PARENT_2 = $TARGET$
		}
	}
	stress_impact = {
		shy = miniscule_stress_impact_gain
		paranoid = miniscule_stress_impact_gain
		craven = miniscule_stress_impact_gain
		chaste = miniscule_stress_impact_gain
	}
}

former_consort_forced_concubine_effect = {
	$PARTNER$ = {
		if = {
			limit = {
				any_consort = {
					count >= 2
				}
			}
			every_consort = { #Updating consort opinions
				if = { #The primary spouse is relieved
					limit = {
						has_opinion_modifier = {
							target = $CONSORT$
							modifier = threatening_consort_opinion
						}
					}
					remove_opinion = {
						target = $CONSORT$
						modifier = threatening_consort_opinion
					}
				}
				if = { #Other consorts don't feel jealous
					limit = {
						has_opinion_modifier = {
							target = $CONSORT$
							modifier = envy_consort_opinion
						}
					}
					remove_opinion = {
						target = $CONSORT$
						modifier = envy_consort_opinion
					}
				}
				if = { #Craven consorts don't feel threatened by a despot
					limit = {
						NOT = {
							this = $CONSORT$
						}
						$CONSORT$ = {
							harem_politics_evil_consort_trigger = yes
						}
						OR = { #Only for the consorts they outranked
							harem_politics_is_valid_authority_trigger = {
								AUTHORITY = $CONSORT$
								TARGET = this
								LIEGE = $PARTNER$
							}
							$CONSORT$ = {
								has_court_position = grand_consort_court_position
							}
						}
					}
					stress_impact = {
						base = minor_stress_impact_loss
						craven = minor_stress_impact_loss
						shy = minor_stress_impact_loss
						paranoid = minor_stress_impact_loss
					}
				}
				if = { #End schemes if any exist
					limit = {
						any_scheme = {
							scheme_target_character = $CONSORT$
						}
					}
					every_scheme = {
						limit = {
							scheme_target_character = $CONSORT$
						}
						end_scheme = yes
					}
				}
				#Trigger event
			}
		}
	}
}

spouse_management_effect = {
	$CONSORT$ = {
		#Divorced
		if = {#As PS
			limit = {
				OR = {
					has_opinion_modifier = {
						modifier = divorced_me_as_ps
						target = $PARTNER$
					}
					has_opinion_modifier = {
						modifier = dismissed_me_as_ps
						target = $PARTNER$
					}
				}
			}

			if = {
				limit = {
					OR = {	
						this = $PARTNER$.primary_spouse
						$PARTNER$ = {
							is_married = no
						}
					}
				}
				#Trigger event and something related to reinstatement
				consort_reinstatement_effect = {
					REINSTATOR = $PARTNER$
					REINSTATED = $CONSORT$
				}
			}
			else = {
				if = {
					limit = {
						has_opinion_modifier = {
							modifier = divorced_me_as_ps
							target = $PARTNER$
						}
					}
					remove_opinion = {
						modifier = divorced_me_as_ps
						target = $PARTNER$
					}
				}
				else = {
					remove_opinion = {
						modifier = dismissed_me_as_ps
						target = $PARTNER$
					}
				}
				add_character_modifier = {
					modifier = demoted_primary_spouse_modifier
				}
				calyver_favor_big_loss_effect = yes
				# Victim is naturally angry for being demoted.
				add_opinion = {
					target = $PARTNER$
					modifier = demoted_me_as_ps
				}
				#Relatives too
				every_close_or_extended_family_member = {
					limit = { NOT = { this = $PARTNER$ } } # don't hate yourself just because of demotion
					custom = all_family_members
					add_opinion = {
						modifier = demoted_concubine_marriage_opinion
						target = $PARTNER$
					}
					send_interface_message = {
						type = event_generic_bad
						title = demoted_grand_consort_t
						right_icon = $CONSORT$
						left_icon = $PARTNER$
						desc = demoted_relative_interface_message
					}
				}
				if = {
					limit = { $PARTNER$.primary_title.tier = tier_county }
					add_prestige = { value = miniscule_prestige_loss }
				}
				else_if = {
					limit = { $PARTNER$.primary_title.tier = tier_duchy }
					add_prestige = { value = minor_prestige_loss }
				}
				else_if = {
					limit = { $PARTNER$.primary_title.tier = tier_kingdom }
					add_prestige = { value = medium_prestige_loss }
				}
				else_if = {
					limit = { $PARTNER$.primary_title.tier = tier_empire }
					add_prestige = { value = major_prestige_loss }
				}
				#Trigger event
				$PARTNER$ = {
					if = { #Legitimacy loss#
						limit = {
							NOT = {
								has_imprisonment_reason = $CONSORT$
							}
							$CONSORT$ = {
								num_sinful_traits = 0
								is_lowborn = no
							}
							is_valid_for_legitimacy_change = yes
						}
						add_legitimacy = minor_legitimacy_loss
					}
					# If we're a clan this interaction affects unity
					add_clan_unity_interaction_effect = {
						CHARACTER = $PARTNER$
						TARGET = $CONSORT$
						VALUE = minor_unity_loss
						DESC = clan_unity_demotion.desc
						REVERSE_NON_HOUSE_TARGET = no
					}
				}
			}
		}
		else_if = {#As GC
			limit = {
				OR = {
					has_opinion_modifier = {
						modifier = divorced_me_as_gc
						target = $PARTNER$
					}
					has_opinion_modifier = {
						modifier = dismissed_me_as_gc
						target = $PARTNER$
					}
				}
			}

			if = {
				limit = {
					OR = {	
						this = $PARTNER$.primary_spouse
						$PARTNER$ = {
							is_married = no
						}
					}
				}
				#Trigger event and something related to promotion
				consort_reinstatement_effect = {
					REINSTATOR = $PARTNER$
					REINSTATED = $CONSORT$
				}
			}
			else = {
				if = {
					limit = {
						has_opinion_modifier = {
							modifier = divorced_me_as_gc
							target = $PARTNER$
						}
					}
					remove_opinion = {
						modifier = divorced_me_as_gc
						target = $PARTNER$
					}
				}
				else = {
					remove_opinion = {
						modifier = dismissed_me_as_gc
						target = $PARTNER$
					}
				}
				add_character_modifier = {
					modifier = demoted_spouse_modifier
				}
				calyver_favor_moderate_loss_effect = yes
				# Victim is naturally angry for being demoted.
				add_opinion = {
					target = $PARTNER$
					modifier = demoted_me_as_gc
				}
				#Relatives too
				every_close_or_extended_family_member = {
					limit = { NOT = { this = $PARTNER$ } } # don't hate yourself just because of demotion
					custom = all_family_members
					add_opinion = {
						modifier = demoted_concubine_marriage_opinion
						target = $PARTNER$
					}
					send_interface_message = {
						type = event_generic_bad
						title = demoted_grand_consort_t
						right_icon = $CONSORT$
						left_icon = $PARTNER$
						desc = demoted_relative_interface_message
					}
				}
				if = {
					limit = { $PARTNER$.primary_title.tier = tier_kingdom }
					add_prestige = { value = minor_prestige_loss }
				}
				else_if = {
					limit = { $PARTNER$.primary_title.tier = tier_empire }
					add_prestige = { value = medium_prestige_loss }
				}
				#Trigger event
				$PARTNER$ = {
					# If we're a clan this interaction affects unity
					add_clan_unity_interaction_effect = {
						CHARACTER = $PARTNER$
						TARGET = $CONSORT$
						VALUE = minor_unity_loss
						DESC = clan_unity_demotion.desc
						REVERSE_NON_HOUSE_TARGET = no
					}
				}
			}
		}
		else_if = {#As SS
			limit = {
				OR = {
					has_opinion_modifier = {
						modifier = divorced_me_as_ss
						target = $PARTNER$
					}
					has_opinion_modifier = {
						modifier = dismissed_me_as_ss
						target = $PARTNER$
					}
				}
			}
			consort_reinstatement_effect = {
				REINSTATOR = $PARTNER$
				REINSTATED = $CONSORT$
			}
			if = {
				limit = {
					this = $PARTNER$.primary_spouse
				}
				#Trigger event and something related to promotion
			}
			else = {
				#Trigger event and something related to reinstatement
			}
		}
		#Demoted
		if = { #As PS
			limit = {
				has_opinion_modifier = {
					modifier = demoted_me_as_ps
					target = $PARTNER$
				}
			}
			if = {
				limit = {
					OR = {	
						this = $PARTNER$.primary_spouse
						$PARTNER$ = {
							is_married = no
						}
					}
				}
				consort_reinstatement_effect = {
					REINSTATOR = $PARTNER$
					REINSTATED = $CONSORT$
				}
				#Trigger reinstatement event
			}
			else = {
				#Trigger event
			}
		}
		else_if = { #As GC
			limit = {
				has_opinion_modifier = {
					modifier = demoted_me_as_gc
					target = $PARTNER$
				}
			}
			if = {
				limit = {
					OR = {	
						this = $PARTNER$.primary_spouse
						$PARTNER$ = {
							is_married = no
						}
					}
				}
				consort_reinstatement_effect = {
					REINSTATOR = $PARTNER$
					REINSTATED = $CONSORT$
				}
				#Trigger promotion event
			}
			else = {
				#Trigger event
			}
		}
		else_if = { #As SS
			limit = {
				has_opinion_modifier = {
					modifier = demoted_me_as_ss
					target = $PARTNER$
				}
			}
			consort_reinstatement_effect = {
				REINSTATOR = $PARTNER$
				REINSTATED = $CONSORT$
			}
			if = { #This should be a promotion effect
				limit = {
					OR = {	
						this = $PARTNER$.primary_spouse
						$PARTNER$ = {
							is_married = no
						}
					}
				}
				#Trigger promotion event
			}
			else = { #This should be a reinstatement
				#Trigger event
			}
		}
	}
} 

spouse_demotion_effect = {
	# Save parameters as scopes for description usage
	$ACTOR$ = { save_scope_as = demotion_actor } #The actor
	$RECIPIENT$ = { save_scope_as = demotion_recipient } #The concubine
	
	hidden_effect = {	
		child_demotion_effect = {
			PARENT_1 = $ACTOR$
			PARENT_2 = $RECIPIENT$
		}
	}
	$ACTOR$ = {
		##House unity and legitimacy effect##
		#PS#
		if = {
			limit = {
				primary_spouse ?= {
					this = $RECIPIENT$
				}
			}
			# If we're a clan this interaction affects unity
			add_clan_unity_interaction_effect = {
				CHARACTER = $ACTOR$
				TARGET = $RECIPIENT$
				VALUE = massive_unity_loss
				DESC = clan_unity_demotion.desc
				REVERSE_NON_HOUSE_TARGET = no
			}
			if = { #Legitimacy loss#
				limit = {
					NOT = {
						has_imprisonment_reason = $RECIPIENT$
					}
					$RECIPIENT$ = {
						num_sinful_traits = 0
						is_lowborn = no
					}
					is_valid_for_legitimacy_change = yes
				}
				add_legitimacy = medium_legitimacy_loss
			}
		}
		else_if = {
			limit = {
				$RECIPIENT$ = {
					OR = {
						has_opinion_modifier = {
							modifier = demoted_me_as_ps
							target = $ACTOR$
						}
						has_opinion_modifier = {
							modifier = divorced_me_as_ps
							target = $ACTOR$
						}
						has_opinion_modifier = {
							modifier = dismissed_me_as_ps
							target = $ACTOR$
						}
					}
				}
			}
			# If we're a clan this interaction affects unity
			add_clan_unity_interaction_effect = {
				CHARACTER = $ACTOR$
				TARGET = $RECIPIENT$
				VALUE = medium_unity_loss
				DESC = clan_unity_demotion.desc
				REVERSE_NON_HOUSE_TARGET = no
			}
			if = { #Legitimacy loss#
				limit = {
					NOT = {
						has_imprisonment_reason = $RECIPIENT$
					}
					$RECIPIENT$ = {
						num_sinful_traits = 0
						is_lowborn = no
					}
					is_valid_for_legitimacy_change = yes
				}
				add_legitimacy = minor_legitimacy_loss
			}
		}
		else_if = { #GC#
			limit = {
				$RECIPIENT$ = {
					is_grand_consort_trigger = yes
				}
			}
			# If we're a clan this interaction affects unity
			add_clan_unity_interaction_effect = {
				CHARACTER = $ACTOR$
				TARGET = $RECIPIENT$
				VALUE = major_unity_loss
				DESC = clan_unity_demotion.desc
				REVERSE_NON_HOUSE_TARGET = no
			}
			if = { #Legitimacy loss#
				limit = {
					NOT = {
						has_imprisonment_reason = $RECIPIENT$
					}
					$RECIPIENT$ = {
						num_sinful_traits = 0
						is_lowborn = no
					}
					is_valid_for_legitimacy_change = yes
				}
				add_legitimacy = minor_legitimacy_loss
			}
		}
		else_if = { #GC#
			limit = {
				$RECIPIENT$ = {
					OR = {
						has_opinion_modifier = {
							modifier = demoted_me_as_gc
							target = $ACTOR$
						}
						has_opinion_modifier = {
							modifier = divorced_me_as_gc
							target = $ACTOR$
						}
						has_opinion_modifier = {
							modifier = dismissed_me_as_gc
							target = $ACTOR$
						}
					}
				}
			}
			# If we're a clan this interaction affects unity
			add_clan_unity_interaction_effect = {
				CHARACTER = $ACTOR$
				TARGET = $RECIPIENT$
				VALUE = minor_unity_loss
				DESC = clan_unity_demotion.desc
				REVERSE_NON_HOUSE_TARGET = no
			}
			if = { #Legitimacy loss#
				limit = {
					NOT = {
						has_imprisonment_reason = $RECIPIENT$
					}
					$RECIPIENT$ = {
						num_sinful_traits = 0
						is_lowborn = no
					}
					is_valid_for_legitimacy_change = yes
				}
				add_legitimacy = minor_legitimacy_loss
			}
		}
		else_if = { #SS#
			limit = {
				$RECIPIENT$ = {
					is_married = yes
				}
			}
			# If we're a clan this interaction affects unity
			add_clan_unity_interaction_effect = {
				CHARACTER = $ACTOR$
				TARGET = $RECIPIENT$
				VALUE = medium_unity_loss
				DESC = clan_unity_demotion.desc
				REVERSE_NON_HOUSE_TARGET = no
			}
			if = { #Legitimacy loss#
				limit = {
					NOT = {
						has_imprisonment_reason = $RECIPIENT$
					}
					$RECIPIENT$ = {
						num_sinful_traits = 0
						is_lowborn = no
					}
					is_valid_for_legitimacy_change = yes
				}
				add_legitimacy = minor_legitimacy_loss
			}
		}
		else_if = {
			limit = {
				$RECIPIENT$ = {
					OR = {
						has_opinion_modifier = {
							modifier = demoted_me_as_ss
							target = $ACTOR$
						}
						has_opinion_modifier = {
							modifier = divorced_me_as_ss
							target = $ACTOR$
						}
						has_opinion_modifier = {
							modifier = dismissed_me_as_ss
							target = $ACTOR$
						}
					}
				}
			}
			# If we're a clan this interaction affects unity
			add_clan_unity_interaction_effect = {
				CHARACTER = $ACTOR$
				TARGET = $RECIPIENT$
				VALUE = minor_unity_loss
				DESC = clan_unity_demotion.desc
				REVERSE_NON_HOUSE_TARGET = no
			}	
			if = { #Legitimacy loss#
				limit = {
					NOT = {
						has_imprisonment_reason = $RECIPIENT$
					}
					$RECIPIENT$ = {
						num_sinful_traits = 0
						is_lowborn = no
					}
					is_valid_for_legitimacy_change = yes
				}
				add_legitimacy = minor_legitimacy_loss
			}
		}
		hidden_effect = {
			every_consort = {
				if = { #Craven consorts don't feel threatened by a despot
					limit = {
						OR = {
							AND = {
								is_concubine = yes
								$RECIPIENT$ = {
									OR = {
										is_married = yes
										is_grand_consort_trigger = yes
									}
								}
							}
							AND = {
								is_married = yes
								$RECIPIENT$ = {
									OR = {
										this = $ACTOR$.primary_spouse
										is_grand_consort_trigger = yes
									}
								}
							}
						}
						NOT = {
							this = $RECIPIENT$
						}
						$RECIPIENT$ = {
							harem_politics_evil_consort_trigger = yes
						}
					}
					stress_impact = {
						craven = minor_stress_impact_loss
						shy = minor_stress_impact_loss
						paranoid = minor_stress_impact_loss
					}
				}
				#Trigger event
			}
		}
	}
	$RECIPIENT$ = {
		#Characters flags and modifiers for events
		#Taking a primary spouse as a concubine
		if = {
			limit = {
				OR = {
					has_opinion_modifier = {
						modifier = demoted_me_as_ps
						target = $ACTOR$
					}
					has_opinion_modifier = {
						modifier = divorced_me_as_ps
						target = $ACTOR$
					}
					has_opinion_modifier = {
						modifier = dismissed_me_as_ps
						target = $ACTOR$
					}
					this = $ACTOR$.primary_spouse
				}
			}
			if = {
				limit = {
					has_opinion_modifier = {
						modifier = divorced_me_as_ps
						target = $ACTOR$
					}
				}
				remove_opinion = {
					modifier = divorced_me_as_ps
					target = $ACTOR$
				}
			}
			if = {
				limit = {
					has_opinion_modifier = {
						modifier = dismissed_me_as_ps
						target = $ACTOR$
					}
				}
				remove_opinion = {
					modifier = dismissed_me_as_ps
					target = $ACTOR$
				}
			}
			hidden_effect = {
				if = { #If for whatever reason, they have another modifier
					limit = {
						has_character_modifier = demoted_spouse_modifier
					}
					remove_character_modifier = demoted_spouse_modifier
				}
			}
			add_character_modifier = {
				modifier = demoted_primary_spouse_modifier
			}
			if = {
				limit = {
					NOT = {
						has_opinion_modifier = {
							modifier = demoted_me_as_ps
							target = $ACTOR$
						}
					}
				}
				add_opinion = {
					target = $ACTOR$
					modifier = demoted_me_as_ps
				}
			}
			if = {
				limit = {
					can_set_relation_rival_trigger = { CHARACTER = $ACTOR$ }
				}
				progress_towards_rival_effect = {
					REASON = demoted_me_reason
					CHARACTER = $ACTOR$
					OPINION = default_rival_opinion
				}
			}
			if = {
				limit = { $ACTOR$.primary_title.tier = tier_county }
				add_prestige = { value = miniscule_prestige_loss }
			}
			else_if = {
				limit = { $ACTOR$.primary_title.tier = tier_duchy }
				add_prestige = { value = minor_prestige_loss }
			}
			else_if = {
				limit = { $ACTOR$.primary_title.tier = tier_kingdom }
				add_prestige = { value = medium_prestige_loss }
			}
			else_if = {
				limit = { $ACTOR$.primary_title.tier = tier_empire }
				add_prestige = { value = major_prestige_loss }
			}
			$ACTOR$ = {
				if = {
					limit = {
						$RECIPIENT$ = {
							is_lowborn = no
						}
						any_vassal = { 
							hp_accepts_harems = yes
							has_vassal_stance = courtly
							count >= 1
						}
					}
					custom_tooltip = demote_consort_courtly_vassal_penalty
					hidden_effect = {
						every_vassal = {
							limit = { 
								hp_accepts_harems = yes
								has_vassal_stance = courtly
							}
							add_opinion = {
								target = $ACTOR$
								modifier = demoted_primary_spouse_vassal_opinion
							}
						}
					}
				}
				
				if = { #A GC spouse should automatically become the PS
					limit = { 
						AND = { 
							$RECIPIENT$ = {
								this = $ACTOR$.primary_spouse
							}
							any_spouse = {
								count >= 2
							}
						}
					}
					new_primary_spouse_divorce_effect = {
						PREVIOUS_PS = $RECIPIENT$
						PARTNER = $ACTOR$
					}
				}
			}
		}
		else_if = { #Taking a Grand Consort as concubine
			limit = {
				OR = {
					has_opinion_modifier = {
						modifier = demoted_me_as_gc
						target = $ACTOR$
					}
					has_opinion_modifier = {
						modifier = divorced_me_as_gc
						target = $ACTOR$
					}
					has_opinion_modifier = {
						modifier = dismissed_me_as_gc
						target = $ACTOR$
					}
					has_court_position = grand_consort_court_position
				}
			}
			if = {
				limit = {
					has_court_position = grand_consort_court_position
				}
				add_character_flag = {
					flag = block_rival_progress_effect_flag
					days = 2
				}
				add_character_flag = {
					flag = block_favor_progress_effect_flag
					days = 2
				}
				$ACTOR$ = {
					revoke_court_position = {
						recipient = $RECIPIENT$
						court_position = grand_consort_court_position
					}
				}
			}
			if = {
				limit = {
					has_opinion_modifier = {
						modifier = divorced_me_as_gc
						target = $ACTOR$
					}
				}
				remove_opinion = {
					modifier = divorced_me_as_gc
					target = $ACTOR$
				}
			}
			if = {
				limit = {
					has_opinion_modifier = {
						modifier = dismissed_me_as_gc
						target = $ACTOR$
					}
				}
				remove_opinion = {
					modifier = dismissed_me_as_gc
					target = $ACTOR$
				}
			}
			add_character_modifier = {
				modifier = demoted_spouse_modifier
			}
			if = {
				limit = {
					NOT = {
						has_opinion_modifier = {
							modifier = demoted_me_as_gc
							target = $ACTOR$
						}
					}
				}
				add_opinion = {
					target = $ACTOR$
					modifier = demoted_me_as_gc
				}
			}
			if = {
				limit = {
					can_set_relation_rival_trigger = { CHARACTER = $ACTOR$ }
				}
				progress_towards_rival_effect = {
					REASON = demoted_me_reason
					CHARACTER = $ACTOR$
					OPINION = default_rival_opinion
				}
			}
			if = {
				limit = { $ACTOR$.primary_title.tier = tier_kingdom }
				add_prestige = { value = minor_prestige_loss }
			}
			else_if = {
				limit = { $ACTOR$.primary_title.tier = tier_empire }
				add_prestige = { value = medium_prestige_loss }
			}
			$ACTOR$ = {
				if = {
					limit = {
						$RECIPIENT$ = {
							is_lowborn = no
						}
						any_vassal = { 
							hp_accepts_harems = yes
							has_vassal_stance = courtly
							count >= 1
						}
					}
					custom_tooltip = demote_consort_courtly_vassal_penalty
					hidden_effect = {
						every_vassal = {
							limit = { 
								hp_accepts_harems = yes
								has_vassal_stance = courtly
							}
							add_opinion = {
								target = $ACTOR$
								modifier = demoted_spouse_vassal_opinion
							}
						}
					}
				}
			}
		}
		else_if = { #Taking a secondary spouse as a concubine
			limit = {
				OR = {
					has_opinion_modifier = {
						modifier = demoted_me_as_ss
						target = $ACTOR$
					}
					has_opinion_modifier = {
						modifier = divorced_me_as_ss
						target = $ACTOR$
					}
					has_opinion_modifier = {
						modifier = dismissed_me_as_ss
						target = $ACTOR$
					}
					is_spouse_of = $ACTOR$
				}
			}
			if = {
				limit = {
					has_opinion_modifier = {
						modifier = divorced_me_as_ss
						target = $ACTOR$
					}
				}
				remove_opinion = {
					modifier = divorced_me_as_ss
					target = $ACTOR$
				}
			}
			if = {
				limit = {
					has_opinion_modifier = {
						modifier = dismissed_me_as_ss
						target = $ACTOR$
					}
				}
				remove_opinion = {
					modifier = dismissed_me_as_ss
					target = $ACTOR$
				}
			}
			add_character_modifier = {
				modifier = demoted_spouse_modifier
			}
			if = {
				limit = {
					NOT = {
						has_opinion_modifier = {
							modifier = demoted_me_as_ss
							target = $ACTOR$
						}
					}
				}
				add_opinion = {
					target = $ACTOR$
					modifier = demoted_me_as_ss
				}
			}
			if = {
				limit = {
					can_set_relation_rival_trigger = { CHARACTER = $ACTOR$ }
				}
				progress_towards_rival_effect = {
					REASON = demoted_me_reason
					CHARACTER = $ACTOR$
					OPINION = default_rival_opinion
				}
			}
			if = {
				limit = { $ACTOR$.primary_title.tier = tier_county }
				add_prestige = { value = miniscule_prestige_loss }
			}
			else_if = {
				limit = { $ACTOR$.primary_title.tier = tier_duchy }
				add_prestige = { value = miniscule_prestige_loss }
			}
			else_if = {
				limit = { $ACTOR$.primary_title.tier = tier_kingdom }
				add_prestige = { value = minor_prestige_loss }
			}
			else_if = {
				limit = { $ACTOR$.primary_title.tier = tier_empire }
				add_prestige = { value = minor_prestige_loss }
			}
			$ACTOR$ = {
				if = {
					limit = {
						$RECIPIENT$ = {
							is_lowborn = no
						}
						any_vassal = { 
							hp_accepts_harems = yes
							has_vassal_stance = courtly
							count >= 1
						}
					}
					custom_tooltip = demote_consort_courtly_vassal_penalty
					hidden_effect = {
						every_vassal = {
							limit = { 
								hp_accepts_harems = yes
								has_vassal_stance = courtly
							}
							add_opinion = {
								target = $ACTOR$
								modifier = demoted_spouse_vassal_opinion
							}
						}
					}
				}
			}
		}
		# Victim is naturally angry for being demoted.
		calyver_favor_big_loss_effect = yes

		clear_positive_consort_modifiers_effect = yes
		if = { #No longer primary opinion
			limit = {
				has_opinion_modifier = {
					target = $ACTOR$
					modifier = spouse_made_primary_opinion
				}
			}
			remove_opinion = {
				modifier = spouse_made_primary_opinion
				target = $ACTOR$
			}
		}
		if = { #No longer secondary opinion
			limit = {
				has_opinion_modifier = {
					target = $ACTOR$
					modifier = spouse_made_secondary_opinion
				}
			}
			remove_opinion = {
				modifier = spouse_made_secondary_opinion
				target = $ACTOR$
			}
		}
		if = { #No longer reinstated opinion
			limit = {
				has_opinion_modifier = {
					target = $ACTOR$
					modifier = reinstated_me_opinion
				}
			}
			remove_opinion = {
				modifier = reinstated_me_opinion
				target = $ACTOR$
			}
		}
		if = { #No longer thankful
			limit = {
				has_opinion_modifier = {
					target = $ACTOR$
					modifier = thankful_consort_opinion
				}
			}
			remove_opinion = {
				target = $ACTOR$
				modifier = thankful_consort_opinion
			}
		}
		
		if = {
			limit = {
				any_close_or_extended_family_member = {
					has_opinion_modifier = {
						target = $ACTOR$
						modifier = reinstated_relative_marriage_opinion
					}
				}
			}
			every_close_or_extended_family_member = {
				limit = { NOT = { this = $ACTOR$ } }
				custom = all_family_members
				remove_opinion = {
					modifier = reinstated_relative_marriage_opinion
					target = $ACTOR$
				}
			}
		}
		every_close_or_extended_family_member = {
			limit = { NOT = { this = $ACTOR$ } } # don't hate yourself just because of demotion
			custom = all_family_members
			add_opinion = {
				modifier = demoted_concubine_marriage_opinion
				target = $ACTOR$
			}
			hidden_effect = {
				send_interface_message = {
					type = event_generic_bad
					title = demoted_relative_marriage
					right_icon = $RECIPIENT$
					left_icon = $ACTOR$
					show_as_tooltip = { $ACTOR$ = { make_concubine = $RECIPIENT$ } }
				}
				if = {
					limit = {
						has_opinion_modifier = {
							target = $ACTOR$
							modifier = thankful_consort_relative_opinion
						}
					}
					remove_opinion = {
						target = $ACTOR$
						modifier = thankful_consort_relative_opinion
					}
				}
			}
		}
	}
	
	$ACTOR$ = {
		hidden_effect = {
			every_consort = {
				if = { #The primary spouse is relieved
					limit = {
						has_opinion_modifier = {
							target = $RECIPIENT$
							modifier = threatening_consort_opinion
						}
					}
					remove_opinion = {
						target = $RECIPIENT$
						modifier = threatening_consort_opinion
					}
				}
				if = { #Other consorts don't feel jealous
					limit = {
						has_opinion_modifier = {
							target = $RECIPIENT$
							modifier = envy_consort_opinion
						}
					}
					remove_opinion = {
						target = $RECIPIENT$
						modifier = envy_consort_opinion
					}
				}
			}
		}
		if = {
			limit = {
				is_spouse_of = $RECIPIENT$
			}
			#Trigger event
			$RECIPIENT$ = {
				trigger_event = marriage_interaction.0030
			}
			divorce = $RECIPIENT$
			hidden_effect = {
				send_interface_toast = {
					type = event_generic_neutral 
					title = msg_concubine_taken_title
					right_icon = $RECIPIENT$
					if = {
						limit = {
							$RECIPIENT$ = {
								NOT = {
									is_concubine_of = $ACTOR$
								}
							}
						}
						make_concubine = $RECIPIENT$
					}
				}
			}
		}
		stress_impact = {
			compassionate = minor_stress_impact_gain
			forgiving = minor_stress_impact_gain
		}
		if = {
			limit = { 
				has_trait = just
				NOT = { has_imprisonment_reason = $RECIPIENT$ }
			}
			stress_impact = {
				just = minor_stress_impact_gain
			}
		}
	}
}

#Divorce effects
divorce_effect = {
	
	# Opinion hit
	$DIVORCEE$ = {
		divorcee_effect = {
			DIVORCER = $DIVORCER$
			DIVORCEE = $DIVORCEE$
		}
		every_close_or_extended_family_member = {
			limit = { NOT = { this = $DIVORCER$ } } # don't hate yourself just because of some incest
			custom = all_family_members
			add_opinion = {
				modifier = divorced_close_kin
				target = $DIVORCER$
			}
		}
		if = {
			limit = {
				has_character_modifier = rejected_from_marriage_bed_modifier
			}
			remove_character_modifier = rejected_from_marriage_bed_modifier
		}
	}
	
	$DIVORCER$ = {
		divorcer_effect = {
			DIVORCER = $DIVORCER$
			DIVORCEE = $DIVORCEE$
		}
		if = {
			limit = {
				any_spouse = {
					this = $DIVORCEE$
				}
			}
			divorce = $DIVORCEE$
		}
		if = {
			limit = {
				has_character_modifier = rejected_from_marriage_bed_modifier
			}
			remove_character_modifier = rejected_from_marriage_bed_modifier
		}
	}
	
	if = {
		limit = { $DIVORCER$ = { has_relation_lover = $DIVORCEE$ } }
		lover_breakup_effect = {
			BREAKER = $DIVORCER$
			LOVER = $DIVORCEE$
		}			
	}
}

divorcee_effect = {
	#Harem Politics effects#
	#Character flags for events and modifiers
	if = { #Primary spouse
		limit = {
			OR = {
				AND = {	
					exists = $DIVORCER$.primary_spouse
					this = $DIVORCER$.primary_spouse
				}
				has_opinion_modifier = {
					modifier = demoted_me_as_ps
					target = $DIVORCER$
				}
			}
		}
		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = $DIVORCER$
			TARGET = $DIVORCEE$
			VALUE = major_unity_loss
			DESC = clan_unity_divorce.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
		if = { #Legitimacy loss#
			limit = {
				$DIVORCER$ = {
					NOT = {
						has_imprisonment_reason = $DIVORCEE$
					}
					is_valid_for_legitimacy_change = yes
				}
				num_sinful_traits = 0
				is_lowborn = no
			}
			$DIVORCER$ = {
				add_legitimacy = medium_legitimacy_loss
			}
		}
		clear_consort_opinions_effect = {
			TARGET = $DIVORCER$
		}
		add_opinion = {
			target = $DIVORCER$
			modifier = divorced_me_as_ps
		}
		if = {
			limit = {
				exists = $DIVORCER$.primary_spouse
				this = $DIVORCER$.primary_spouse
				$DIVORCER$ = {
					any_spouse = {
						count >= 2
					}
				}
			}
			$DIVORCER$ = {
				new_primary_spouse_divorce_effect = {
					PREVIOUS_PS = $DIVORCEE$
					PARTNER = $DIVORCER$
				}
			}
		}
	}
	else_if = { #Grand Consort
		limit = {
			OR = {
				is_grand_consort_trigger = yes
				has_opinion_modifier = {
					modifier = demoted_me_as_gc
					target = $DIVORCER$
				}
			}
		}
		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = $DIVORCER$
			TARGET = $DIVORCEE$
			VALUE = medium_unity_loss
			DESC = clan_unity_divorce.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
		if = { #Legitimacy loss#
			limit = {
				$DIVORCER$ = {
					NOT = {
						has_imprisonment_reason = $DIVORCEE$
					}
					is_valid_for_legitimacy_change = yes
				}
				num_sinful_traits = 0
				is_lowborn = no
			}
			$DIVORCER$ = {
				add_legitimacy = minor_legitimacy_loss
			}
		}
		if = {
			limit = {
				has_court_position = grand_consort_court_position
				is_courtier_of = $DIVORCER$
			}
			add_character_flag = {
				flag = block_rival_progress_effect_flag
				days = 2
			}
			add_character_flag = {
				flag = block_favor_progress_effect_flag
				days = 2
			}
			$DIVORCER$ = {
				revoke_court_position = {
					recipient = $DIVORCEE$
					court_position = grand_consort_court_position
				}
			}
		}
		clear_consort_opinions_effect = {
			TARGET = $DIVORCER$
		}
		add_opinion = {
			target = $DIVORCER$
			modifier = divorced_me_as_gc
		}
	}
	else = { #Regular secondary spouse
		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = $DIVORCER$
			TARGET = $DIVORCEE$
			VALUE = minor_unity_loss
			DESC = clan_unity_divorce.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
		if = { #Legitimacy loss#
			limit = {
				$DIVORCER$ = {
					NOT = {
						has_imprisonment_reason = $DIVORCEE$
					}
					is_valid_for_legitimacy_change = yes
				}
				num_sinful_traits = 0
				is_lowborn = no
			}
			$DIVORCER$ = {
				add_legitimacy = minor_legitimacy_loss
			}
		}
		clear_consort_opinions_effect = {
			TARGET = $DIVORCER$
		}
		add_opinion = {
			target = $DIVORCER$
			modifier = divorced_me_as_ss
		}
	}
	if = {
		limit = { $DIVORCER$.primary_title.tier = tier_county }
		add_prestige = { value = miniscule_prestige_loss }
	}
	else_if = {
		limit = { $DIVORCER$.primary_title.tier = tier_duchy }
		add_prestige = { value = minor_prestige_loss }
	}
	else_if = {
		limit = { $DIVORCER$.primary_title.tier = tier_kingdom }
		add_prestige = { value = medium_prestige_loss }
	}
	else_if = {
		limit = { $DIVORCER$.primary_title.tier = tier_empire }
		add_prestige = { value = major_prestige_loss }
	}
	
	#Removing demoted modifiers and flags
	clear_demoted_consort_divorce_modifiers_effect = yes

	if = {
		limit = {
			any_close_or_extended_family_member = {
				has_opinion_modifier = {
					target = $DIVORCER$
					modifier = reinstated_relative_marriage_opinion
				}
			}
		}
		every_close_or_extended_family_member = {
			limit = { NOT = { this = $DIVORCER$ } }
			custom = all_family_members
			remove_opinion = {
				modifier = reinstated_relative_marriage_opinion
				target = $DIVORCER$
			}
		}
	}
	
	#Progress towards rivals
	if = {
		limit = {
			can_set_relation_rival_trigger = { CHARACTER = $DIVORCER$ }
		}
		progress_towards_rival_effect = {
			REASON = divorced_me_reason
			CHARACTER = $DIVORCER$
			OPINION = default_rival_opinion
		}
	}
	
	hidden_effect = {
		#Update consort opinion
		every_consort = {
			if = { #The primary spouse is relieved
				limit = {
					has_opinion_modifier = {
						target = $DIVORCER$
						modifier = threatening_consort_opinion
					}
				}
				remove_opinion = {
					target = $DIVORCER$
					modifier = threatening_consort_opinion
				}
			}
			if = { #Other consorts don't feel jealous
				limit = {
					has_opinion_modifier = {
						target = $DIVORCER$
						modifier = envy_consort_opinion
					}
				}
				remove_opinion = {
					target = $DIVORCER$
					modifier = envy_consort_opinion
				}
			}
			if = { #Craven consorts don't feel threatened by a despot
				limit = {
					OR = {
						is_concubine = yes
						AND = {
							is_married = yes
							$DIVORCER$ = {
								OR = {
									this = $DIVORCEE$.primary_spouse
									is_grand_consort_trigger = yes
								}
							}
						}
					}
					NOT = {
						this = $DIVORCER$
					}
					$DIVORCEE$ = {
						harem_politics_evil_consort_trigger = yes
					}
				}
				stress_impact = {
					craven = minor_stress_impact_loss
					shy = minor_stress_impact_loss
					paranoid = minor_stress_impact_loss
				}
			}
			#Trigger event
		}
		every_close_or_extended_family_member = {
			limit = {
				AND = {
					NOT = { this = $DIVORCER$ }
					has_opinion_modifier = {
						target = $DIVORCER$
						modifier = thankful_consort_relative_opinion
					}
				}
			}
			remove_opinion = {
				target = $DIVORCER$
				modifier = thankful_consort_relative_opinion
			}
		}
	}
}

divorcer_effect = {
	#Harem Politics effects#
	if = {
		limit = {
			AND = {	
				exists = $DIVORCEE$.primary_spouse
				this = $DIVORCEE$.primary_spouse
			}
			$DIVORCEE$ = {
				any_spouse = {
					count >= 2
				}
			}
		}
		$DIVORCEE$ = {
			new_primary_spouse_divorce_effect = {
				PREVIOUS_PS = $DIVORCER$
				PARTNER = $DIVORCEE$
			}
		}
	}
	
	if = {
		limit = { $DIVORCEE$.primary_title.tier = tier_county }
		add_prestige = { value = miniscule_prestige_loss }
	}
	else_if = {
		limit = { $DIVORCEE$.primary_title.tier = tier_duchy }
		add_prestige = { value = minor_prestige_loss }
	}
	else_if = {
		limit = { $DIVORCEE$.primary_title.tier = tier_kingdom }
		add_prestige = { value = medium_prestige_loss }
	}
	else_if = {
		limit = { $DIVORCEE$.primary_title.tier = tier_empire }
		add_prestige = { value = major_prestige_loss }
	}
	
	#Removing demoted modifiers and flags
	clear_demoted_consort_divorce_modifiers_effect = yes
	
	clear_consort_opinions_effect = {
		TARGET = $DIVORCEE$
	}

	hidden_effect = {
		#Update consort opinion
		every_consort = {
			if = { #The primary spouse is relieved
				limit = {
					has_opinion_modifier = {
						target = $DIVORCEE$
						modifier = threatening_consort_opinion
					}
				}
				remove_opinion = {
					target = $DIVORCEE$
					modifier = threatening_consort_opinion
				}
			}
			if = { #Other consorts don't feel jealous
				limit = {
					has_opinion_modifier = {
						target = $DIVORCEE$
						modifier = envy_consort_opinion
					}
				}
				remove_opinion = {
					target = $DIVORCEE$
					modifier = envy_consort_opinion
				}
			}
			if = { #Craven consorts don't feel threatened by a despot
				limit = {
					OR = {
						is_concubine = yes
						AND = {
							is_married = yes
							$DIVORCEE$ = {
								OR = {
									this = $DIVORCER$.primary_spouse
									is_grand_consort_trigger = yes
								}
							}
						}
					}
					NOT = {
						this = $DIVORCEE$
					}
					$DIVORCEE$ = {
						harem_politics_evil_consort_trigger = yes
					}
				}
				stress_impact = {
					craven = minor_stress_impact_loss
					shy = minor_stress_impact_loss
					paranoid = minor_stress_impact_loss
				}
			}
			#Trigger event
		}
	}
}

new_primary_spouse_divorce_effect = {
	$PARTNER$ = {
		if = { 
			limit = {
				any_court_position_holder = {
					type = grand_consort_court_position
					NOT = {	
						this = $PREVIOUS_PS$
					}
				}
			}
			random_court_position_holder = {
				type = grand_consort_court_position
				save_scope_as = new_primary_spouse
			}
			if = {
				limit = {
					NOT = {
						primary_spouse ?= scope:new_primary_spouse
					}
				}
				set_primary_spouse = scope:new_primary_spouse
			}
			make_primary_spouse_effect = {
				PARTNER = $PARTNER$
			}
			clear_saved_scope = new_primary_spouse
		}
		else = {
			hidden_effect = {
				every_spouse = {
					limit = { 	
						NOT = {	
							this = $PREVIOUS_PS$
						}
					}
					add_to_temporary_list = future_primary_spouse
				}
				random_in_list = {
					list = future_primary_spouse
					weight = {
						base = 100
						modifier = {
							faith = $PARTNER$.faith
							add = 20
						}
						modifier = {
							culture = $PARTNER$.culture
							add = 10
						}
						modifier = {
							is_demoted_consort_trigger = no
							add = 30
						}
						modifier = {
							is_lowborn = no
							add = 50
						}
						modifier = {
							any_close_or_extended_family_member = {
								is_landed = yes
							}
							add = 20
						}
						modifier = {
							any_close_family_member = {
								highest_held_title_tier = tier_county
							}
							add = 15
						}
						modifier = {
							any_close_family_member = {
								highest_held_title_tier = tier_duchy
							}
							add = 25
						}
						modifier = {
							any_close_family_member = {
								highest_held_title_tier = tier_kingdom
							}
							add = 35
						}
						modifier = {
							any_close_family_member = {
								highest_held_title_tier = tier_empire
							}
							add = 45
						}
						modifier = {
							any_close_family_member = {
								is_vassal_of = $PARTNER$
							}
							add = 10
						}
						modifier = {
							any_close_family_member = {
								is_powerful_vassal_of = $PARTNER$
							}
							add = 35
						}
						modifier = {
							any_close_family_member = {
								is_councillor_of = $PARTNER$
							}
							add = 30
						}
						modifier = {
							any_close_family_member = {
								is_liege_or_above_of = $PARTNER$
							}
							add = 500
						}
						
						modifier = {
							NOT = { has_character_flag = flag_new_consort }
							add = 15
						}
						modifier = {
							has_character_modifier = forgotten_consort_modifier
							add = -20
						}
						modifier = {
							has_character_modifier = unfavored_consort_modifier
							add = -10
						}
						modifier = {
							has_character_modifier = favored_consort_modifier
							add = 5
						}
						modifier = {
							has_character_modifier = favor_boost_modifier
							add = 10
						}
						modifier = {
							has_character_modifier = beloved_consort_modifier
							add = 15
						}
						modifier = {
							num_virtuous_traits >= 1
							add = {
								value = num_virtuous_traits
								multiply = 5
							}
						}
						modifier = {
							num_sinful_traits >= 1
							add = {
								value = num_sinful_traits
								multiply = -5
							}
						}
						modifier = {
							any_secret = {
								exists = secret_owner
								is_known_by = $PARTNER$
								is_criminal_for = $PARTNER$
							}
							add = -20
						}
						modifier = {
							OR = {
								has_relation_rival = $PARTNER$
								has_relation_nemesis = $PARTNER$
								has_relation_potential_rival = $PARTNER$
							}
							add = -25
						}

						modifier = {
							add = {
								every_child = {
									limit = {
										is_child_of = $PARTNER$
									}
									add = {
										value = 1
										if = {
											limit = {
												character_gender_can_inherit_from_trigger = { CHARACTER = $PARTNER$ }
											}
											add = 1
										}
										if = {
											limit = {
												is_player_heir_of = $PARTNER$
											}
											add = 2
										}
									}
								}
							}
						}
					}
					save_scope_as = new_primary_spouse
				}
				if = {
					limit = {
						NOT = {
							primary_spouse ?= scope:new_primary_spouse
						}
					}
					set_primary_spouse = scope:new_primary_spouse
				}
				make_primary_spouse_effect = {
					PARTNER = $PARTNER$
				}
				clear_saved_scope = new_primary_spouse
			}
		}
	}
}

less_verbose_divorce_effect = {
	hidden_effect = {
		# Opinion hit
		$DIVORCEE$ = {
			divorcee_effect = {
				DIVORCER = $DIVORCER$
				DIVORCEE = $DIVORCEE$
			}
			every_close_or_extended_family_member = {
				limit = { NOT = { this = $DIVORCER$ } } # don't hate yourself just because of some incest
				custom = all_family_members
				add_opinion = {
					modifier = divorced_close_kin
					target = $DIVORCER$
				}
			}
			if = {
				limit = {
					has_character_modifier = rejected_from_marriage_bed_modifier
				}
				remove_character_modifier = rejected_from_marriage_bed_modifier
			}
		}
	}
	$DIVORCER$ = {
		hidden_effect = {
			if = {
				limit = {
					has_character_modifier = rejected_from_marriage_bed_modifier
				}
				remove_character_modifier = rejected_from_marriage_bed_modifier
			}
			divorcer_effect = {
				DIVORCER = $DIVORCER$
				DIVORCEE = $DIVORCEE$
			}
		}
		if = {
			limit = {
				any_spouse = {
					this = $DIVORCEE$
				}
			}
			divorce = $DIVORCEE$
		}
	}
	
	if = {
		limit = { $DIVORCER$ = { has_relation_lover = $DIVORCEE$ } }
		lover_breakup_effect = {
			BREAKER = $DIVORCER$
			LOVER = $DIVORCEE$
		}
	}
}

#Setting a new primary spouse
set_primary_spouse_effect = {
	if = {
		limit = {
			scope:actor.primary_spouse ?= {
				NOT = {
					this = scope:recipient
				}
			}
		}
		scope:actor.primary_spouse = {
			calyver_favor_big_loss_effect = yes
			if = {
				limit = {
					has_opinion_modifier = {
						target = scope:actor
						modifier = spouse_made_primary_opinion
					}
				}
				remove_opinion = {
					modifier = spouse_made_primary_opinion
					target = scope:actor
				}
			}
			if = {
				limit = {
					has_opinion_modifier = {
						target = scope:actor
						modifier = reinstated_me_opinion
					}
				}
				remove_opinion = {
					modifier = reinstated_me_opinion
					target = scope:actor
				}
			}
			if = { #No longer thankful
				limit = {
					has_opinion_modifier = {
						target = scope:actor
						modifier = thankful_consort_opinion
					}
				}
				remove_opinion = {
					target = scope:actor
					modifier = thankful_consort_opinion
				}
			}
			add_opinion = {
				target = scope:actor
				modifier = demoted_me_as_ps
			}
			add_character_modifier = {
				modifier = demoted_primary_spouse_modifier
			}
			if = {
				limit = {
					can_set_relation_rival_trigger = { CHARACTER = scope:actor }
				}
				progress_towards_rival_effect = {
					REASON = demoted_me_reason
					CHARACTER = scope:actor
					OPINION = default_rival_opinion
				}
			}
			if = {
				limit = {
					any_close_or_extended_family_member = {
						has_opinion_modifier = {
							target = scope:actor
							modifier = reinstated_relative_marriage_opinion
						}
					}
				}
				every_close_or_extended_family_member = {
					limit = { NOT = { this = scope:actor } }
					custom = all_family_members
					remove_opinion = {
						modifier = reinstated_relative_marriage_opinion
						target = scope:actor
					}
				}
			}
			if = {
				limit = {
					any_close_or_extended_family_member = {
						has_opinion_modifier = {
							target = scope:actor
							modifier = thankful_consort_relative_opinion
						}
					}
				}
				every_close_or_extended_family_member = {
					limit = { NOT = { this = scope:actor } }
					custom = all_family_members
					remove_opinion = {
						modifier = thankful_consort_relative_opinion
						target = scope:actor
					}
				}
			}
			every_close_or_extended_family_member = {
				limit = { NOT = { this = scope:actor } } # don't hate yourself just because of demotion
				custom = all_family_members
				add_opinion = {
					modifier = demoted_concubine_marriage_opinion
					target = scope:actor
				}
			}
			hidden_effect = {
				if = {
					limit = {
						can_set_relation_rival_trigger = { CHARACTER = scope:recipient }
					}
					progress_towards_rival_effect = {
						REASON = consort_rival_reason
						CHARACTER = scope:recipient
						OPINION = 0
					}
				}
				add_opinion = {
					target = scope:recipient
					modifier = hatred_spouse_opinion
				}
				clear_positive_consort_modifiers_effect = yes
				send_interface_toast = {
					type = event_toast_effect_bad
					title = set_primary_spouse_interaction_toast
					left_icon = scope:actor
					right_icon = scope:recipient

					custom_tooltip = set_primary_spouse_interaction_toast_removed
				}
			}
			if = {
				limit = { scope:actor.primary_title.tier = tier_county }
				add_prestige = { value = miniscule_prestige_loss }
			}
			else_if = {
				limit = { scope:actor.primary_title.tier = tier_duchy }
				add_prestige = { value = minor_prestige_loss }
			}
			else_if = {
				limit = { scope:actor.primary_title.tier = tier_kingdom }
				add_prestige = { value = medium_prestige_loss }
			}
			else_if = {
				limit = { scope:actor.primary_title.tier = tier_empire }
				add_prestige = { value = major_prestige_loss }
			}
			save_scope_as = previous_primary_spouse
			# If we're a clan this interaction affects unity
			add_clan_unity_interaction_effect = {
				CHARACTER = scope:actor
				TARGET = scope:previous_primary_spouse 
				VALUE = medium_unity_loss
				DESC = clan_unity_demotion.desc
				REVERSE_NON_HOUSE_TARGET = no
			}
		}
	}
	scope:actor = {
		stress_impact = {
			compassionate = minor_stress_impact_gain
			forgiving = minor_stress_impact_gain
		}
		if = {
			limit = { 
				has_trait = just
				exists = primary_spouse
				NOT = { has_imprisonment_reason = primary_spouse }
			}
			stress_impact = {
				just = minor_stress_impact_gain
			}
		}
		if = {
			limit = { 
				has_trait = just
				has_imprisonment_reason = scope:recipient
			}
			stress_impact = {
				just = minor_stress_impact_loss
			}
		}
		# New and previous GC management
		if = {
			limit = {
				scope:previous_primary_spouse ?= {
					has_court_position = grand_consort_court_position
				}
			}
			hidden_effect = {
				revoke_court_position = {
					recipient = scope:previous_primary_spouse
					court_position = grand_consort_court_position
				}
			}
			set_primary_spouse = scope:recipient
			if = {
				limit = {
					can_appoint_char_to_court_position = {
						CHAR = scope:recipient
						COURT_POS = grand_consort_court_position
					}
				}
				appoint_court_position = {
					recipient = scope:recipient
					court_position = grand_consort_court_position
				}
				scope:recipient = {
					add_character_flag = {
						flag = appointed_gc_ps_flag
						days = 2
					}
				}
			}
		}
		else = {
			set_primary_spouse = scope:recipient
		}
		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_gain
			DESC = clan_unity_promotion.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
		hidden_effect = {	
			scope:recipient = {
				save_scope_as = new_primary_spouse
			}
			every_consort = {
				calyver_downgrade_active_consort_opinion_effect = yes
				#Consorts will update their opinions on new primary_spouse
				if = {
					limit = {
						scope:recipient = {
							NOT = { 
								has_demoted_or_divorced_as_ps_opinion_trigger = {
									TARGET = scope:actor
								}
							}
						}
						harem_politics_evil_consort_trigger = yes
						NOR = {
							has_relation_best_friend = scope:recipient
							has_relation_friend = scope:recipient
							has_relation_soulmate = scope:recipient
							has_relation_lover = scope:recipient
							has_relation_soulmate = scope:actor
							this = scope:recipient
							has_opinion_modifier = {
								modifier = forced_me_concubine_marriage_opinion
								target = scope:actor
							}
							AND = {
								exists = scope:previous_primary_spouse
								this = scope:previous_primary_spouse
							}
						}
						trigger_if = {
							limit = {
								hp_accepts_harems = no
							}
							OR = {
								has_trait = ambitious
								has_trait = arrogant
								has_trait = greedy
							}
						}
						trigger_else = {
							hp_accepts_harems = yes
						}
						opinion = {
							target = scope:recipient
							value <= low_positive_opinion
						}
					}
					add_opinion = {
						target = scope:recipient
						modifier = envy_consort_opinion
					}
				}
				if = {
					limit = {
						NOT = {
							this = scope:recipient
						}
						scope:recipient = {
							harem_politics_evil_consort_trigger = yes
						}
					}
					stress_impact = {
						craven = minor_stress_impact_gain
						shy = minor_stress_impact_gain
						paranoid = minor_stress_impact_gain
					}
				}
				if = {
					limit = {
						NOT = {
							this = scope:recipient
						}
					}
					#Trigger event
				}
			}
			# Managing GC opinions
			if = {
				limit = {
					employs_court_position = grand_consort_court_position
					any_court_position_holder = {
						type = grand_consort_court_position
						NOR = {
							this = scope:recipient
							this = scope:previous_primary_spouse
						}
					}
				}
				random_court_position_holder = {
					type = grand_consort_court_position
					save_scope_as = grand_consort_scope
				}
				if = {
					limit = {
						exists = scope:grand_consort_scope
					}
					if = {
						limit = {
							scope:previous_primary_spouse ?= {
								has_opinion_modifier = {
									target = scope:grand_consort_scope
									modifier = threatening_consort_opinion
								}
							}
						}
						scope:previous_primary_spouse = {
							remove_opinion = {
								target = scope:grand_consort_scope
								modifier = threatening_consort_opinion
							}
						}
					}
					if = {
						limit = {
							scope:recipient = {
								OR = {
									has_trait = craven
									has_trait = paranoid
									has_trait = shy

									harem_politics_evil_consort_trigger = yes

									has_relation_rival = scope:grand_consort_scope
									has_relation_nemesis = scope:grand_consort_scope
								}
								NOR = {
									has_relation_best_friend = scope:grand_consort_scope
									has_relation_friend = scope:grand_consort_scope
									has_relation_soulmate = scope:grand_consort_scope
									has_relation_lover = scope:grand_consort_scope

									has_trait = trusting
								}
								opinion = {
									target = scope:grand_consort_scope
									value <= low_positive_opinion
								}
							}
						}
						scope:recipient = {
							add_opinion = {
								target = scope:grand_consort_scope
								modifier = threatening_consort_opinion
							}
						}
					}
					scope:grand_consort_scope = { #The Grand Consort expects to be the next PS.
						calyver_favor_moderate_loss_effect = yes
						if = {
							limit = {
								harem_politics_evil_consort_trigger = yes
								opinion = {
									target = scope:recipient
									value <= 10
								}
								NOR = {
									has_opinion_modifier = {
										modifier = forced_me_concubine_marriage_opinion
										target = scope:actor
									}
									has_demoted_or_divorced_as_ps_opinion_trigger = {
										TARGET = scope:actor
									}
								}
								trigger_if = {
									limit = {
										hp_accepts_harems = no
									}
									OR = {
										has_trait = ambitious
										has_trait = arrogant
										has_trait = greedy
									}
								}
								trigger_else = {
									hp_accepts_harems = yes
								}
							}
							add_opinion = {
								target = scope:actor
								modifier = ignored_consort_opinion
								opinion = -15
							}
						}
					}
				}
			}
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = set_primary_spouse_interaction_toast
				left_icon = scope:actor
				right_icon = scope:new_primary_spouse

				custom_tooltip = set_primary_spouse_interaction_toast_changed
			}
		}
		#Vassals opinion
		if = { #Do they have the same status?
			limit = {
				scope:previous_primary_spouse ?= {
					is_lowborn = no
				}
				scope:recipient = {
					is_lowborn = no
				}
			}
			every_vassal_or_below = {
				limit = { has_vassal_stance = courtly }
				custom = every_courtly_vassal
				add_opinion = {
					target = scope:actor
					modifier = demoted_primary_spouse_vassal_opinion
				}
			}
			every_vassal_or_below = {
				limit = { has_vassal_stance = glory_hound }
				custom = every_glory_hound_vassal
				add_opinion = {
					target = scope:actor
					modifier = demoted_primary_spouse_vassal_opinion
				}
			}
		}
		else_if = { #Does the previous one outrank the new one?
			limit = {
				scope:previous_primary_spouse ?= {
					is_lowborn = no
				}
				scope:recipient = {
					is_lowborn = yes
				}
			}
			every_vassal_or_below = {
				limit = { has_vassal_stance = courtly }
				custom = every_courtly_vassal
				add_opinion = {
					target = scope:actor
					modifier = demoted_primary_spouse_vassal_opinion
					opinion = -50
				}
			}
			every_vassal_or_below = {
				limit = { has_vassal_stance = glory_hound }
				custom = every_glory_hound_vassal
				add_opinion = {
					target = scope:actor
					modifier = demoted_primary_spouse_vassal_opinion
					opinion = -30
				}
			}
		}
	}
	make_primary_spouse_effect = {
		PARTNER = scope:actor
	}
}

make_primary_spouse_effect = {
	$PARTNER$ = {
		save_scope_as = demoter
	}
	scope:new_primary_spouse = {
		hidden_effect = {	
			send_interface_toast = {
				type = event_toast_effect_good
				title = set_primary_spouse_interaction_toast
				left_icon = scope:demoter
				right_icon = scope:new_primary_spouse

				custom_tooltip = set_primary_spouse_interaction_toast_added

				clear_negative_consort_modifiers_effect = yes
				if = {
					limit = {
						NOT = {
							has_demoted_or_divorced_as_ps_opinion_trigger = {
								TARGET = $PARTNER$
							}
						}
					}
					if = {
						limit = {
							NOT = {
								has_character_flag = appointed_gc_ps_flag
							}
						}
						calyver_favor_big_gain_effect = yes
					}
					else = {
						calyver_favor_moderate_gain_effect = yes
						remove_character_flag = appointed_gc_ps_flag
					}
				}
			}
		}
		
		if = {
			limit = {
				NOT = {
					has_demoted_or_divorced_as_ps_opinion_trigger = {
						TARGET = $PARTNER$
					}
				}
			}
			if = {
				limit = {
					hp_accepts_harems = yes
				}
				add_opinion = {
					target = $PARTNER$
					modifier = spouse_made_primary_opinion
				}
			}
			hidden_effect = {
				every_close_or_extended_family_member = {
					limit = {
						NOT = {
							this = $PARTNER$
						}
					}
					send_interface_message = {
						type = event_generic_good_text 
						title = grand_consort_promotion_t 
						right_icon = scope:new_primary_spouse
						left_icon = $PARTNER$
						show_as_tooltip = {
							$PARTNER$ = {
								set_primary_spouse = scope:new_primary_spouse
							}
						}
					}
					if = {
						limit = {
							NOR = {
								has_opinion_modifier = {
									target = $PARTNER$
									modifier = forced_family_concubine_marriage_opinion
								}
								has_opinion_modifier = {
									target = $PARTNER$
									modifier = thankful_consort_relative_opinion
								}
							}
							hp_accepts_harems = yes
						}
						add_opinion = {
							target = $PARTNER$
							modifier = thankful_consort_relative_opinion
						}
					}
				}
			}
		}
		
		if = { #Character flags
			limit = {
				has_demoted_or_divorced_opinion_trigger = {
					TARGET = $PARTNER$
				}
			}

			consort_reinstatement_effect = {
				REINSTATOR = $PARTNER$
				REINSTATED = scope:new_primary_spouse
			}
		}
		
		if = {
			limit = { $PARTNER$.primary_title.tier = tier_county }
			add_prestige = { value = miniscule_prestige_gain }
		}
		else_if = {
			limit = { $PARTNER$.primary_title.tier = tier_duchy }
			add_prestige = { value = minor_prestige_gain }
		}
		else_if = {
			limit = { $PARTNER$.primary_title.tier = tier_kingdom }
			add_prestige = { value = medium_prestige_gain }
		}
		else_if = {
			limit = { $PARTNER$.primary_title.tier = tier_empire }
			add_prestige = { value = major_prestige_gain }
		}
		
		$PARTNER$ = {
			#Legitimacy loss
			if = {
				limit = {
					is_valid_for_legitimacy_change = yes
				}
				if = { #Do they have the same status?
					limit = {
						scope:previous_primary_spouse ?= {
							is_lowborn = no
						}
						scope:new_primary_spouse = {
							is_lowborn = no
						}
					}
					add_legitimacy = minor_legitimacy_loss
				}
				else_if = { #Does the previous PS outrank the new one?
					limit = {
						scope:previous_primary_spouse ?= {
							is_lowborn = no
						}
						scope:new_primary_spouse = {
							is_lowborn = yes
						}
					}
					add_legitimacy = {
						value = medium_legitimacy_loss
						multiply = primary_title.tier
					}
				}
				else = {
					#Legitimacy gain from marrying into a higher rank
					marriage_legitimacy_rank_effect = {
						RULER = $PARTNER$
						OTHER = scope:new_primary_spouse
					}
				}
			}
			hidden_effect = {
				every_consort = {
					if = { #Opinions
						limit = {
							NOR = {
								has_relation_best_friend = scope:new_primary_spouse
								has_relation_friend = scope:new_primary_spouse
								has_relation_soulmate = scope:new_primary_spouse
								has_relation_lover = scope:new_primary_spouse

								has_relation_best_friend = $PARTNER$
								has_relation_soulmate = $PARTNER$

								this = scope:new_primary_spouse
								
								AND = {
									exists = scope:previous_primary_spouse
									this = scope:previous_primary_spouse
								}
							}
						}
						if = { #Envy
							limit = {
								harem_politics_evil_consort_trigger = yes
								trigger_if = {
									limit = {
										hp_accepts_harems = no
									}
									OR = {
										has_trait = ambitious
										has_trait = arrogant
										has_trait = greedy
									}
								}
								trigger_else = {
									hp_accepts_harems = yes
								}
								NOT = {
									has_opinion_modifier = {
										modifier = envy_consort_opinion
										target = scope:new_primary_spouse
									}
								}
							}
							add_opinion = {
								target = scope:new_primary_spouse
								modifier = envy_consort_opinion
							}
						}
						if = { #AI
							limit = {
								is_ai = no
							}
							send_interface_toast = {
								type = event_generic_bad
								title = new_primary_spouse_t
								right_icon = scope:new_primary_spouse
								left_icon = scope:demoter
								custom_tooltip = new_primary_spouse_desc
								if = { #Stress
									limit = {
										scope:new_primary_spouse = {
											harem_politics_evil_consort_trigger = yes
										}
									}
									stress_impact = {
										craven = minor_stress_impact_gain
										shy = minor_stress_impact_gain
										paranoid = minor_stress_impact_gain
									}
								}
							}
						}
						if = { #Stress
							limit = {
								is_ai = yes
								scope:new_primary_spouse = {
									harem_politics_evil_consort_trigger = yes
								}
							}
							stress_impact = {
								craven = minor_stress_impact_gain
								shy = minor_stress_impact_gain
								paranoid = minor_stress_impact_gain
							}
						}
					}
				}
			}
		}
	}
}

#Updating consorts' opinions
calyver_update_active_consort_opinion_effect = {

	if = {
		# If this opinion change was triggered by being divorced/set-aside, we may still technically be married/joined to PARTNER at this moment, but we won't be very shortly and should skip this step.
		limit = {
			exists = $SPOUSE$
			NOR = {
				exists = scope:is_being_divorced
				exists = scope:is_being_set_aside
				this = $SPOUSE$
			}
		}
		
		#Concubines will get jealous of every new spouse
		if = {
			limit = {		
				is_concubine = yes
			}
			if = {
				limit = {
					has_court_position = grand_consort_court_position
					is_court_position_employer = {
						court_position = grand_consort_court_position
						who = $PARTNER$
					}
					NOT = {
						has_demoted_or_divorced_as_ps_opinion_trigger = {
							TARGET = $PARTNER$
						}
					}
				}
				calyver_favor_moderate_loss_effect = yes
			}
			if = {
				limit = {
					NOR = {
						has_relation_best_friend = $SPOUSE$
						has_relation_friend = $SPOUSE$
						has_relation_soulmate = $SPOUSE$
						has_relation_lover = $SPOUSE$
					}
					$PARTNER$ = {
						OR = {
							primary_spouse ?= {
								this = $SPOUSE$
							}
							$SPOUSE$ = {
								NOT = {
									has_demoted_or_divorced_opinion_trigger = {
										TARGET = $PARTNER$
									}
								}
							}
						}
					}
				}
				# Managing opinions
				if = {
					limit = {
						harem_politics_evil_consort_trigger = yes
						opinion = {
							target = $SPOUSE$
							value <= 15
						}
						NOR = {
							has_demoted_or_divorced_as_ps_opinion_trigger = {
								TARGET = $PARTNER$
							}
							has_opinion_modifier = {
								modifier = forced_me_concubine_marriage_opinion
								target = $PARTNER$
							}
							has_relation_rival = $PARTNER$
							has_relation_nemesis = $PARTNER$
						}
						trigger_if = {
							limit = {
								hp_accepts_harems = no
							}
							OR = {
								has_trait = ambitious
								has_trait = arrogant
								has_trait = greedy
							}
						}
						trigger_else = {
							hp_accepts_harems = yes
						}
					}
					if = { # The GC expects to be the next PS
						limit = {
							has_court_position = grand_consort_court_position
							is_court_position_employer = {
								court_position = grand_consort_court_position
								who = $PARTNER$
							}
						}
						add_opinion = {
							target = $PARTNER$
							modifier = ignored_consort_opinion
							opinion = -15
						}
					}
					# Envy
					if = {
						limit = {
							NOT = {
								has_opinion_modifier = {
									target = $SPOUSE$
									modifier = envy_consort_opinion
								}
							}
						}
						add_opinion = {
							target = $SPOUSE$
							modifier = envy_consort_opinion
						}
					}
				}
				# Stress
				if = {
					limit = {		
						$SPOUSE$ = {
							harem_politics_evil_consort_trigger = yes
						}
					}
					stress_impact = {
						craven = minor_stress_impact_gain
						shy = minor_stress_impact_gain
						paranoid = minor_stress_impact_gain
					}
				}
			}
		}
	}
}

calyver_downgrade_active_consort_opinion_effect = {
	if = {
		# If this opinion change was triggered by being divorced/set-aside, we may still technically be married/joined to PARTNER at this moment, but we won't be very shortly and should skip this step.
		limit = {
			NOR = {
				exists = scope:is_being_divorced
				exists = scope:is_being_set_aside
			}
		}
		#Consorts will update their opinions on previous primary spouse
		if = {
			limit = {
				exists = scope:previous_primary_spouse
			}
			if = {
				limit = {
					has_opinion_modifier = {
						target = scope:previous_primary_spouse
						modifier = envy_consort_opinion
					}
				}
				remove_opinion = {
					target = scope:previous_primary_spouse
					modifier = envy_consort_opinion
				}
			}
			if = { #Craven consorts don't feel threatened by a despot primary spouse
				limit = {
					NOT = {
						this = scope:previous_primary_spouse
					}
					scope:previous_primary_spouse = {
						harem_politics_evil_consort_trigger = yes
					}
				}
				stress_impact = {
					craven = minor_stress_impact_loss
					shy = minor_stress_impact_loss
					paranoid = minor_stress_impact_loss
				}
			}
		}
	}
}

# When you take spouses/concubines whose Faith does not permit that union, they will gain opinion penalties.
# This effect adds/updates/replaces/removes those opinion penalties, as approprite to the current situation.
update_active_consort_opinion_effect = {

	# If we currently have any existing consort opinion modifiers, check if they should be downgraded from permanent modifiers to decaying modifiers.
	downgrade_invalid_consort_opinions_effect = { PARTNER = $PARTNER$ }

	if = {
		# If this opinion change was triggered by being divorced/set-aside, we may still technically be married/joined to PARTNER at this moment, but we won't be very shortly and should skip this step.
		limit = {
			NOR = {
				exists = scope:is_being_divorced
				exists = scope:is_being_set_aside
			}
		}

		# Oterwise, apply any new consort opinion effects that are applicable.
		if = {
			# Characters who do not believe in concubinage, but are forced into it, have the harshest opinion penalties.
			limit = {
				is_concubine_of = $PARTNER$
				NOT = { hp_accepts_concubinage = yes }
			}
			if = {
				# Harshest opinion penalty if they are a concubine but believe in monogamy
				limit = { 
					hp_accepts_concubinage = no
					hp_accepts_polygamy = no
				}
				add_opinion = {
					target = $PARTNER$
					modifier = concubine_with_monogamous_faith_opinion
				}
			}
			else_if = {
				# Lesser-but-still-harsh opinion penalty if they are a concubine but believe in polygamy
				limit = {
					hp_accepts_polygamy = yes
				}
				add_opinion = {
					target = $PARTNER$
					modifier = concubine_with_polygamous_faith_opinion
				}
			}
		}
		else_if = {
			# Characters who are legitimate spouses of their partner, but do not approve of their partner's other spouses/concubines, will have milder opinion penalties.
			limit = {
				is_spouse_of = $PARTNER$
				$PARTNER$ = { any_consort = { count >= 2 }}
				NOT = { hp_accepts_concubinage = yes }
				NOT = { faith = { has_doctrine = tenet_polyamory }}
			}
			if = {
				limit = {
					$PARTNER$ = { any_concubine = { count >= 1 }}
				}
				add_opinion = {
					target = $PARTNER$
					modifier = spouse_does_not_believe_in_concubines_opinion
				}
				#Remove the opinion modifier if you flip-flop and go get concubines after getting rid of them all...
				remove_opinion = {
					target = $PARTNER$
					modifier = spouse_does_not_believe_in_former_concubines_opinion
				}
			}
			else_if = {
				limit = {
					hp_accepts_polygamy = no
				}
				add_opinion = {
					target = $PARTNER$
					modifier = polygamous_marriage_opinion
				}
			}
		}
		#Same-sex not accepted penalty
		if = {
			limit = {
				allowed_to_marry_same_sex_trigger = no
				sex_same_as = $PARTNER$
			}
			add_opinion = {
				target = $PARTNER$
				modifier = same_sex_with_no_acceptance_opinion
			}
		}
	}
}

# Checks if we have any permanent opinion modifiers which are no longer applicable. If so, downgrade them to decaying opinion modifiers.
downgrade_invalid_consort_opinions_effect = {
	if = {
		limit = {
			has_opinion_modifier = {
				modifier = concubine_with_monogamous_faith_opinion
				target = $PARTNER$
			}
			OR = {
				# We are no longer a concubine of PARTNER...
				NOT = { is_concubine_of = $PARTNER$ }
				# ...or will no longer be a concubine of them.
				trigger_if = {
					limit = { exists = scope:is_being_set_aside}
					scope:is_being_set_aside = this
				}

				# Or, our Faith/culture has changed to permit us being a concubine.
				hp_accepts_concubinage = yes
			}
		}
		remove_opinion = {
			modifier = concubine_with_monogamous_faith_opinion
			target = $PARTNER$
		}
		add_opinion = {
			modifier = formerly_concubine_with_monogamous_faith_opinion
			target = $PARTNER$
		}
	}

	if = {
		limit = {
			has_opinion_modifier = {
				modifier = concubine_with_polygamous_faith_opinion
				target = $PARTNER$
			}
			OR = {
				# We are no longer a concubine of PARTNER...
				NOT = { is_concubine_of = $PARTNER$ }
				# ...or will no longer be a concubine of them.
				trigger_if = {
					limit = { exists = scope:is_being_set_aside}
					scope:is_being_set_aside = this
				}

				# Or, our Faith/Culture has changed to permit us being a concubine.
				hp_accepts_concubinage = yes
			}
		}
		remove_opinion = {
			modifier = concubine_with_polygamous_faith_opinion
			target = $PARTNER$
		}
		add_opinion = {
			modifier = formerly_concubine_with_polygamous_faith_opinion
			target = $PARTNER$
		}
	}

	if = {
		limit = {
			has_opinion_modifier = {
				modifier = spouse_does_not_believe_in_concubines_opinion
				target = $PARTNER$
			}
			OR = {
				# We are no longer married to PARTNER...
				NOT = { is_spouse_of = $PARTNER$ }
				# ...or will no longer be married to them.
				trigger_if = {
					limit = { exists = scope:is_being_divorced}
					scope:is_being_divorced = this
				}

				# Or, the offending relationship no longer exists.
				$PARTNER$ = { any_concubine = {	count = 0 }}
				AND = {
					# The last remaining concubine is being set aside, leaving none.
					exists = scope:is_being_set_aside
					$PARTNER$ = { any_concubine = { count = 1 }}
				}

				# Or, our Faith/Culture has changed to permit the relationship.
				hp_accepts_concubinage = yes
				faith = { has_doctrine = tenet_polyamory }
			}
		}
		remove_opinion = {
			modifier = spouse_does_not_believe_in_concubines_opinion
			target = $PARTNER$
		}
		add_opinion = {
			modifier = spouse_does_not_believe_in_former_concubines_opinion
			target = $PARTNER$
		}
	}

	if = {
		limit = {
			has_opinion_modifier = {
				modifier = polygamous_marriage_opinion
				target = $PARTNER$
			}
			OR = {
				# We are no longer married to PARTNER...
				NOT = { is_spouse_of = $PARTNER$ }
				# ...or will no longer be married to them.
				trigger_if = {
					limit = { exists = scope:is_being_divorced}
					scope:is_being_divorced = this
				}

				# Or, the offending relationship no longer exists.
				$PARTNER$ = { any_spouse = { count = 1 }}
				AND = {
					# One of the 2 remaining spouses will be divorced next tick, leaving only 1.
					exists = scope:is_being_divorced 
					$PARTNER$ = { any_spouse = { count = 2 }}
				}

				# Or, our Faith/Culture has changed to permit the relationship.
				hp_accepts_concubinage = yes
				hp_accepts_polygamy = yes
				faith = { has_doctrine = tenet_polyamory }
			}
		}
		remove_opinion = {
			modifier = polygamous_marriage_opinion
			target = $PARTNER$
		}
		add_opinion = {
			modifier = former_polygamous_marriage_opinion
			target = $PARTNER$
		}
	}

	if = {
		limit = {
			has_opinion_modifier = {
				modifier = same_sex_with_no_acceptance_opinion
				target = $PARTNER$
			}
			OR = {
				# We are no partner of PARTNER...
				NOT = { is_consort_of = $PARTNER$ }
				# ...or will no longer be married to them
				trigger_if = {
					limit = { exists = scope:is_being_divorced}
					scope:is_being_divorced = this
				}
				# ...or will no longer be a concubine of them
				trigger_if = {
					limit = { exists = scope:is_being_set_aside}
					scope:is_being_set_aside = this
				}

				# Or, the offending relationship no longer exists.
				sex_opposite_of = $PARTNER$

				# Or, our Faith has changed to permit the relationship.
				allowed_to_marry_same_sex_trigger = yes
			}
		}
		remove_opinion = {
			modifier = same_sex_with_no_acceptance_opinion
			target = $PARTNER$
		}
		add_opinion = {
			modifier = former_same_sex_with_no_acceptance_opinion
			target = $PARTNER$
		}
	}
}

new_primary_spouse_on_action_effect = {
	save_scope_as = harem_liege
	if = { #The Grand Consort is promoted
		limit = {
			employs_court_position = grand_consort_court_position
			any_court_position_holder = {
				type = grand_consort_court_position
				NOT = {	
					this = scope:dead_relation
				}
			}
		}
		random_court_position_holder = {
			type = grand_consort_court_position
			save_scope_as = new_primary_spouse
		}
	}
	else = {
		ordered_spouse = {
			limit = {
				NOT = {
					this = scope:dead_relation
				}
			}
			order_by = {
				value = 1
				add = primary_spouse_score_value
			}
			save_scope_as = new_primary_spouse
		}
	}
	if = {
		limit = {
			NOT = {
				primary_spouse ?= scope:new_primary_spouse
			}
		}
		set_primary_spouse = scope:new_primary_spouse
	}
	scope:new_primary_spouse = {
		clear_negative_consort_modifiers_effect = yes
		if = {
			limit = {
				has_demoted_or_divorced_opinion_trigger = {
					TARGET = scope:harem_liege
				}
			}
			consort_reinstatement_effect = {
				REINSTATOR = scope:harem_liege
				REINSTATED = scope:new_primary_spouse
			}
			#Trigger event
		}
		else = {
			calyver_favor_huge_gain_effect = yes
			if = {
				limit = {
					hp_accepts_harems = yes
					NOT = {
						has_demoted_or_divorced_as_ps_opinion_trigger = {
							TARGET = scope:harem_liege
						}
					}
				}
				add_opinion = {
					target = scope:harem_liege
					modifier = spouse_made_primary_opinion
				}
			}
			#Trigger event
		}
	}
}

consort_reinstatement_effect = {
	#Modifiers
	clear_demoted_consort_modifiers_effect = yes
	#Opinions
	clear_consort_opinions_effect = {
		TARGET = $REINSTATOR$
	}

	calyver_favor_moderate_gain_effect = yes
	add_opinion = {
		modifier = reinstated_me_opinion
		target = $REINSTATOR$
	}
	every_close_or_extended_family_member = {
		limit = { NOT = { this = $REINSTATOR$ } }
		custom = all_family_members
		add_opinion = {
			modifier = reinstated_relative_marriage_opinion
			target = $REINSTATOR$
		}
	}
	# If we're a clan this interaction affects unity
	add_clan_unity_interaction_effect = {
		CHARACTER = $REINSTATOR$
		TARGET = $REINSTATED$
		VALUE = minor_unity_gain
		DESC = clan_unity_promotion.desc
		REVERSE_NON_HOUSE_TARGET = no
	}
}

calyver_dismiss_concubine_effect = {
	scope:recipient = {
		#Add appropriate opinions
		if = {
			limit = {
				OR = {
					has_opinion_modifier = {
						modifier = demoted_me_as_ps
						target = scope:actor
					}
					has_opinion_modifier = {
						modifier = divorced_me_as_ps
						target = scope:actor
					}
				}
			}
			clear_consort_opinions_effect = {
				TARGET = scope:actor
			}
			add_opinion = {
				target = scope:actor
				modifier = dismissed_me_as_ps
			}
		}	
		else_if = {
			limit = {
				OR = {
					is_grand_consort_trigger = yes
					has_opinion_modifier = {
						modifier = demoted_me_as_gc
						target = scope:actor
					}
					has_opinion_modifier = {
						modifier = divorced_me_as_gc
						target = scope:actor
					}
				}
			}
			if = {
				limit = {
					is_grand_consort_trigger = yes
				}
				save_scope_as = employee
				scope:actor = {
					save_scope_as = liege
				}
				grand_consort_revoked_court_position_effect = yes
			}
			clear_consort_opinions_effect = {
				TARGET = scope:actor
			}
			add_opinion = {
				target = scope:actor
				modifier = dismissed_me_as_gc
			}
		}
		else_if = {
			limit = {
				OR = {
					has_opinion_modifier = {
						modifier = demoted_me_as_ss
						target = scope:actor
					}
					has_opinion_modifier = {
						modifier = divorced_me_as_ss
						target = scope:actor
					}
				}
			}
			clear_consort_opinions_effect = {
				TARGET = scope:actor
			}
			add_opinion = {
				target = scope:actor
				modifier = dismissed_me_as_ss
			}
		}
		else = {
			add_opinion = {
				modifier = set_me_aside_opinion
				target = scope:actor
			}
		}

		#Removing demoted modifiers and flags
		clear_demoted_consort_divorce_modifiers_effect = yes
		
		if = { #Event
			limit = {
				is_ai = no
			}
			trigger_event = marriage_interaction.0050
		}

		if = {
			limit = { scope:actor = { has_relation_lover = scope:recipient } }
			lover_breakup_effect = {
				BREAKER = scope:actor
				LOVER = scope:recipient
			}			
		}	

		# Figure out where we should go now that we're no longer a concubine.
		if = {
			limit = {
				is_councillor_of = scope:actor
			}
			# Then they should remain in court
		}
		else_if = { #Dismiss to relevant court, or to pool
			limit = {
				any_close_family_member = {
					is_landed_or_landless_administrative = yes
				}
			}
			random_close_family_member = {
				limit = {
					is_child_of = scope:recipient
					is_landed_or_landless_administrative = yes
				}
				alternative_limit = {
					is_parent_of = scope:recipient
					is_landed_or_landless_administrative = yes
				}
				alternative_limit = {
					is_landed_or_landless_administrative = yes
				}
				add_courtier = scope:recipient
			}
		}
		else = {
			move_to_pool = yes
		}
	}		
	scope:actor = {
		stress_impact = {
			compassionate = minor_stress_impact_gain
			forgiving = minor_stress_impact_gain
		}
		hidden_effect = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = msg_concubine_dismissed_title
				right_icon = scope:recipient
				remove_concubine = scope:recipient
			}
		}
	}
	# If we're a clan this interaction affects unity
	add_clan_unity_interaction_effect = {
		CHARACTER = scope:actor
		TARGET = scope:recipient
		VALUE = medium_unity_loss
		DESC = clan_unity_concubinage_dismissal.desc
		REVERSE_NON_HOUSE_TARGET = no
	}
}

clear_demoted_consort_divorce_modifiers_effect = {
	clear_demoted_consort_modifiers_effect = yes
	
	clear_positive_consort_modifiers_effect = yes
	clear_negative_consort_modifiers_effect = yes
	calyver_remove_consort_flags_effect = yes
	if = {
		limit = {
			has_character_modifier = mourner_modifier
		}
		remove_character_modifier = mourner_modifier
	}
	if = {
		limit = {
			has_character_modifier = merry_mourner_modifier
		}
		remove_character_modifier = merry_mourner_modifier
	}
	if = {
		limit = { 
			has_character_flag = flag_new_consort 
		}
		remove_character_flag = flag_new_consort
	}
	if = {
		limit = {
			has_variable = favor
		}
		remove_variable = favor	
	}
}

clear_demoted_consort_modifiers_effect = {
	if = { #Demoted primary spouses
		limit = { 
			has_character_modifier = demoted_primary_spouse_modifier 
		}
		remove_character_modifier = demoted_primary_spouse_modifier
	}
	if = { #Demoted spouses
		limit = { 
			has_character_modifier = demoted_spouse_modifier 
		}
		remove_character_modifier = demoted_spouse_modifier
	}
	
	if = {
		limit = { has_character_flag = flag_scolded_consort }
		remove_character_flag = flag_scolded_consort
	}
}

clear_consort_opinions_effect = {
	if = {
		limit = {
			has_opinion_modifier = {
				modifier = divorced_me_as_ps
				target = $TARGET$
			}
		}
		remove_opinion = {
			modifier = divorced_me_as_ps
			target = $TARGET$
		}
	}
	if = {
		limit = {
			has_opinion_modifier = {
				modifier = demoted_me_as_ps
				target = $TARGET$
			}
		}
		remove_opinion = {
			modifier = demoted_me_as_ps
			target = $TARGET$
		}
	}
	if = {
		limit = {
			has_opinion_modifier = {
				modifier = dismissed_me_as_ps
				target = $TARGET$
			}
		}
		remove_opinion = {
			modifier = dismissed_me_as_ps
			target = $TARGET$
		}
	}

	if = {
		limit = {
			has_opinion_modifier = {
				modifier = divorced_me_as_gc
				target = $TARGET$
			}
		}
		remove_opinion = {
			modifier = divorced_me_as_gc
			target = $TARGET$
		}
	}
	if = {
		limit = {
			has_opinion_modifier = {
				modifier = demoted_me_as_gc
				target = $TARGET$
			}
		}
		remove_opinion = {
			modifier = demoted_me_as_gc
			target = $TARGET$
		}
	}
	if = {
		limit = {
			has_opinion_modifier = {
				modifier = dismissed_me_as_gc
				target = $TARGET$
			}
		}
		remove_opinion = {
			modifier = dismissed_me_as_gc
			target = $TARGET$
		}
	}

	if = {
		limit = {
			has_opinion_modifier = {
				modifier = divorced_me_as_ss
				target = $TARGET$
			}
		}
		remove_opinion = {
			modifier = divorced_me_as_ss
			target = $TARGET$
		}
	}
	if = {
		limit = {
			has_opinion_modifier = {
				modifier = demoted_me_as_ss
				target = $TARGET$
			}
		}
		remove_opinion = {
			modifier = demoted_me_as_ss
			target = $TARGET$
		}
	}
	if = {
		limit = {
			has_opinion_modifier = {
				modifier = dismissed_me_as_ss
				target = $TARGET$
			}
		}
		remove_opinion = {
			modifier = dismissed_me_as_ss
			target = $TARGET$
		}
	}

	if = {
		limit = {
			has_opinion_modifier = {
				modifier = set_me_aside_opinion
				target = $TARGET$
			}
		}
		remove_opinion = {
			modifier = set_me_aside_opinion
			target = $TARGET$
		}
	}

	if = { #No longer thankful
		limit = {
			has_opinion_modifier = {
				target = $TARGET$
				modifier = thankful_consort_opinion
			}
		}
		remove_opinion = {
			target = $TARGET$
			modifier = thankful_consort_opinion
		}
	}

	if = { #No longer primary opinion
		limit = {
			has_opinion_modifier = {
				target = $TARGET$
				modifier = spouse_made_primary_opinion
			}
		}
		remove_opinion = {
			modifier = spouse_made_primary_opinion
			target = $TARGET$
		}
	}
	if = { #No longer secondary opinion
		limit = {
			has_opinion_modifier = {
				target = $TARGET$
				modifier = spouse_made_secondary_opinion
			}
		}
		remove_opinion = {
			modifier = spouse_made_secondary_opinion
			target = $TARGET$
		}
	}
	if = { #No longer reinstated opinion
		limit = {
			has_opinion_modifier = {
				target = $TARGET$
				modifier = reinstated_me_opinion
			}
		}
		remove_opinion = {
			modifier = reinstated_me_opinion
			target = $TARGET$
		}
	}
}

concubine_offer_on_accept_effect = {
	scope:secondary_actor = {
		trigger_event = marriage_interaction.0040
		if = {
			limit = {
				religion = religion:islam_religion
				NOR = {
					has_character_flag = abducted_non_believer
					is_concubine = yes
				}
				is_imprisoned = no
			}
			scope:actor = {
				if = {
					limit = {
						religion = religion:islam_religion
					}
					add_piety = medium_piety_loss
				}
			}
			scope:recipient = {
				if = {
					limit = {
						religion = religion:islam_religion
					}
					add_piety = medium_piety_loss
				}	
			}
		}
		if = {
			# Victim is angry for being forced into concubinage.
			limit = {
				OR = {
					has_trait = celibate
					is_imprisoned = yes
				}
			}
			hidden_effect = {
				if = {
					limit = {
						any_consort = {
							NOT = { this = scope:recipient }
							count >= 1
						}
					}
					every_consort = {
						limit = {
							NOT = { this = scope:recipient }
						}
						scope:secondary_actor = {
							former_consort_forced_concubine_effect = {
								PARTNER = prev
								CONSORT = scope:secondary_actor
							}
						}
					}
				}
			}
			add_character_flag = {
				flag = block_release_from_prison_event
				days = 3
			}
			if = {
				limit = {
					is_imprisoned = yes
				}
				release_from_prison = yes
			}
			add_opinion = {
				target = scope:actor
				modifier = forced_me_concubine_marriage_opinion
			}

			# Family members of victim are also angry that you've forced them into concubinage.
			every_close_family_member = {
				limit = { 
					NOT = { this = scope:actor } # Don't hate yourself.
				}
				custom = all_family_members
				add_opinion = {
					target = scope:actor
					modifier = forced_family_concubine_marriage_opinion
				}
				hidden_effect = {
					send_interface_message = {
						type = msg_family_became_concubine
						title = msg_close_family_member_taken_as_concubine_title
						right_icon = scope:actor
						left_icon = scope:secondary_actor
						show_as_tooltip = { scope:actor = { make_concubine = scope:secondary_actor } }
					}
				}

				# Family members are even *more* angry if they don't believe in concubines/polyamory (different from polygamy). Also if they don't accept same-sex relationships
				if = {
					limit = { 
						OR = {
							NOT = { hp_accepts_concubinage = yes }
							NOT = { faith = { has_doctrine = tenet_polyamory } }
							AND = {
								allowed_to_marry_same_sex_trigger = no
								scope:recipient = { sex_same_as = scope:secondary_actor }
							}
						}
					}
					add_opinion = {
						target = scope:actor
						modifier = relative_in_blasphemous_union_opinion
					}
				}
			}
				
			# Not up to them anymore...
			if = {
				limit = { has_trait = celibate }
				remove_trait = celibate
			}
			
			# Spouses, if any exist, are also furious.
			if = {
				limit = {
					is_married = yes
					#A check to make sure you haven't somehow married them during the consideration time
					NOT = { any_spouse = { this = scope:recipient } }
				}
				every_spouse = {
					hidden_effect = {
						if = {
							limit = {
								primary_spouse ?= {
									this = scope:secondary_actor
								}
								any_spouse = {
									count >= 2
								}
							}
							add_character_flag = {
								flag = flag_lowborn_exception
								days = 2
							}
							scope:secondary_actor = {
								new_primary_spouse_divorce_effect = {
									PREVIOUS_PS = scope:secondary_actor
									PARTNER = prev
								}
							}
							remove_character_flag = flag_lowborn_exception
						}
					}
					add_opinion = {
						target = scope:actor
						modifier = forced_spouse_concubine_marriage_opinion
					}
					divorce = scope:secondary_actor # no additional opinion hit, no scripted effect
					save_scope_value_as = { # For tooltips in the coming event
						name = was_spouse
						value = yes
					}
					trigger_event = marriage_interaction.0043
				}
			}
					
			# Stealing concubines pisses the former concubinist off.
			if = {
				limit = {
					is_concubine = yes
					#A check to make sure you haven't somehow gotten them as concubine during the consideration time
					NOT = { any_consort = { this = scope:recipient } }
				}
				this.concubinist = {
					remove_concubine = scope:secondary_actor # no additional opinion hit, no scripted effect
					if = {
						limit = {
							NOT = {
								this = scope:actor
							}
						}
						add_opinion = {
							target = scope:actor
							modifier = stole_concubine_opinion
						}
						save_scope_value_as = { # For tooltips in the coming event
							name = was_concubine
							value = yes
						}
						trigger_event = marriage_interaction.0043
					}
				}
			}

			# Break any betrothals which may exist (without additional opinion penalties).
			if = {
				limit = {
					exists = betrothed
					#A check to make sure you haven't somehow gotten betrothed to them during the consideration time
					NOT = { betrothed = scope:recipient }
				}
				betrothed = { trigger_event = marriage_interaction.0043 }
				break_betrothal = betrothed
			}

			# Having concubines means they're now no longer yours.
			if = {
				limit = { number_of_concubines > 0	}
				every_concubine = {
					scope:secondary_actor = {
						remove_concubine = prev
					}
				}
			}
			clear_demoted_consort_divorce_modifiers_effect = yes
		}
		make_concubine_harem_politics_effect = {
			CHARACTER = scope:recipient
			TARGET = scope:secondary_actor
		}
		if = {
			limit = {
				is_concubine_of = scope:actor
			}
			scope:actor = {
				remove_concubine = scope:secondary_actor
			}
		}
		remove_character_flag = has_been_offered_as_concubine
	}

	# Send us a notification about what we just did. If the secondary_actor somehow ended up as a consort (spouse/concubine) or betrothed to recipient during the consideration time of this request we'll send a message about that instead (should only happen to AI tho)
	if = {
		limit = { #Invalidation message
			scope:secondary_actor = {
				OR = {
					any_consort = { count >= 1 }
					exists = betrothed
				}
			}
		}
		scope:secondary_actor = {
			# If they've somehow already become entangled with the recipient we tell this
			if = {
				limit = {
					OR = {
						is_consort_of = scope:recipient
						AND = {
							exists = betrothed
							betrothed = scope:recipient
						}
					}
				}
				scope:recipient = {
					save_scope_as = spouse
				}
			}
			# Else, if they've married or gotten betrothed to someone else, we say this
			else_if = {
				limit = {
					exists = betrothed
				}
				betrothed = {
					save_scope_as = spouse
				}
			}
			else = {
				random_consort = {
					limit = {
						this = scope:secondary_actor.primary_spouse
					}
					alternative_limit = {
						always = yes
					}
					save_scope_as = spouse
				}
			}
		}
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = msg_concubine_offered_invalidated_title
				right_icon = scope:recipient
				left_icon = scope:secondary_actor
				if = {
					limit = {
						scope:secondary_actor = { is_consort_of = scope:spouse }
					}
					custom_tooltip = msg_concubine_offered_invalidated_consort
				}
				else = {
					custom_tooltip = msg_concubine_offered_invalidated_betrothed
				}
			}
		}
		scope:recipient = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = msg_concubine_offered_invalidated_recipient_title
				right_icon = scope:actor
				left_icon = scope:secondary_actor
				if = {
					limit = {
						scope:secondary_actor = { is_consort_of = scope:spouse }
					}
					custom_tooltip = msg_concubine_offered_invalidated_consort
				}
				else = {
					custom_tooltip = msg_concubine_offered_invalidated_betrothed
				}
			}
		}
	}
	else = { #Proper notification
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = msg_concubine_offered_title
				right_icon = scope:recipient
				left_icon = scope:secondary_actor
				scope:recipient = {
					make_concubine = scope:secondary_actor
					if = { #Scale opinion gain a bit depending on how cool the concubine is
						limit = {
							scope:secondary_actor = {
								OR = {
									has_trait = beauty_good_2
									has_trait = beauty_good_3
									num_of_good_genetic_traits > 1
									has_relation_lover = scope:recipient
									has_relation_soulmate = scope:recipient
								}
								trigger_if = {
									limit = {
										is_female = yes
									}
									age < define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
								}
							}
						}
						add_opinion = {
							target = scope:actor
							modifier = grateful_opinion
							opinion = 40
						}
					}
					else_if = {
						limit = {
							scope:secondary_actor = {
								OR = {
									is_lowborn = no
									has_trait = beauty_good_1
									num_of_good_genetic_traits > 0
									has_relation_friend = scope:recipient
									has_relation_best_friend = scope:recipient
								}
								trigger_if = {
									limit = {
										is_female = yes
									}
									age < define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
								}
							}
						}
						add_opinion = {
							target = scope:actor
							modifier = grateful_opinion
							opinion = 30
						}
					}
					else = {
						add_opinion = {
							target = scope:actor
							modifier = grateful_opinion
							opinion = 20
						}
					}
				}

				if = {
					limit = {
						NOT = { has_character_flag = received_influence_offer_concubine_flag }
					}
					gain_influence_harem_politics_effect = {
						RECIPIENT = scope:actor
						CHARACTER = scope:recipient
						TARGET = scope:secondary_actor
					}
					add_character_flag = {
						flag = received_influence_offer_concubine_flag
						years = 3
					}
				}
			}
		}
	}
}

vassals_dislike_marry_lowborn_effect = {
	$RULER$ = {
		if = {
			limit = {
				is_lowborn = no
				$SPOUSE$ = { is_lowborn = yes }
			}
			if = {
				limit = {
					OR = {
						NOT = { exists = primary_spouse }
						primary_spouse ?= $SPOUSE$
					}
				}
				every_vassal_or_below = {
					limit = { has_vassal_stance = courtly }
					custom = every_courtly_vassal
					add_opinion = {
						modifier = married_lowborn_opinion
						target = $RULER$
						opinion = -50
					}
				}
				every_vassal_or_below = {
					limit = { has_vassal_stance = glory_hound }
					custom = every_glory_hound_vassal
					add_opinion = {
						modifier = married_lowborn_opinion
						target = $RULER$
						opinion = -30
					}	
				}
			}
			else_if = {
				limit = {
					primary_spouse ?= {
						NOT = {
							this = $SPOUSE$
						}
					}
				}
				every_vassal_or_below = {
					limit = { has_vassal_stance = courtly }
					custom = every_courtly_vassal
					add_opinion = {
						modifier = married_lowborn_opinion
						target = $RULER$
						opinion = -20
					}
				}
				every_vassal_or_below = {
					limit = { has_vassal_stance = glory_hound }
					custom = every_glory_hound_vassal
					add_opinion = {
						modifier = married_lowborn_opinion
						target = $RULER$
						opinion = -10
					}
				}
			}	
		}
	}
}

marriage_legitimacy_rank_effect = {
    # RULER = character getting legitimacy (actor in marriage/PARTNER in PS change)
    # OTHER = character whose relatives we check (spouse/PS being evaluated)
    
    if = {
        limit = {
            $RULER$ = {
                is_valid_for_legitimacy_change = yes
                NOR = { 
					has_character_flag = marriage_legitimacy_flag 
					government_has_flag = government_is_nomadic
				}
            }
            $OTHER$ = {
                is_lowborn = no
                OR = {
                    valid_marriage_legitimacy_rank_trigger = { RULER = $RULER$ }
                    any_close_family_member = {
                        valid_marriage_legitimacy_rank_trigger = { RULER = $RULER$ }
                    }
                }
            }
        }
        
        # Add OTHER to the list if they qualify
        $OTHER$ = {
            if = {
                limit = {
                    valid_marriage_legitimacy_rank_trigger = { RULER = $RULER$ }
                }
                add_to_temporary_list = ranked_candidates
            }
            
            # Add OTHER's close family members to the list if they qualify
            if = {
                limit = {
                    any_close_family_member = {
                        valid_marriage_legitimacy_rank_trigger = { RULER = $RULER$ }
                    }
                }
                every_close_family_member = {
                    limit = {
                        valid_marriage_legitimacy_rank_trigger = { RULER = $RULER$ }
                    }
                    add_to_temporary_list = ranked_candidates
                }
            }
        }
        
        # Get highest ranked character from the list
        ordered_in_list = {
            list = ranked_candidates
            order_by = highest_held_title_tier
            max = 1
            save_scope_as = highest_ranked_relative
        }
        
        # Apply legitimacy gain based on rank difference
        $RULER$ = {
            if = {
                limit = {
                    scope:highest_ranked_relative = {
                        tier_difference = {
                            target = $RULER$
                            value > 0
                        }
                    }
                }
                add_legitimacy = {
                    value = medium_legitimacy_gain
                    multiply = scope:highest_ranked_relative.highest_held_title_tier
                }
            }
            else = {
                # Same rank or special case for emperor marrying king/emperor
                add_legitimacy = {
                    value = minor_legitimacy_gain
                    multiply = scope:highest_ranked_relative.highest_held_title_tier
                }
            }
            add_character_flag = marriage_legitimacy_flag
        }
    }
}

#This effect has some small differences AGAIN to the previous one since this one has some roles reversed
demand_concubine_interaction_on_accept_effect = {
	scope:secondary_actor = {
		trigger_event = marriage_interaction.0040
		if = {
			# Victim is angry for being forced into concubinage.
			limit = {
				OR = {
					has_trait = celibate
					is_imprisoned = yes
				}
			}
			hidden_effect = {
				if = {
					limit = {
						any_consort = {
							NOT = { this = scope:actor }
							count >= 1
						}
					}
					every_consort = {
						limit = {
							NOT = { this = scope:actor }
						}
						scope:secondary_actor = {
							former_consort_forced_concubine_effect = {
								PARTNER = prev
								CONSORT = scope:secondary_actor
							}
						}
					}
				}
			}
			if = {
				limit = {
					is_imprisoned = yes
				}
				add_character_flag = {
					flag = block_release_from_prison_event
					days = 3
				}
				release_from_prison = yes
			}
			add_opinion = {
				target = scope:actor
				modifier = forced_me_concubine_marriage_opinion
			}
		}

		# Family members of victim are also angry that you've forced them into concubinage.
		every_close_family_member = {
			limit = { 
				NOT = { this = scope:actor } # Don't hate yourself.
			}
			custom = all_family_members
			add_opinion = {
				target = scope:actor
				modifier = forced_family_concubine_marriage_opinion
			}
			hidden_effect = {
				send_interface_message = {
					type = msg_family_became_concubine
					title = msg_close_family_member_taken_as_concubine_title
					right_icon = scope:actor
					left_icon = scope:secondary_actor
					show_as_tooltip = { scope:actor = { make_concubine = scope:secondary_actor } }
				}
			}

			# Family members are even *more* angry if they don't believe in concubines/polyamory (different from polygamy). Also if they don't accept same-sex relationships
			if = {
				limit = { 
					OR = {
						NOT = { hp_accepts_concubinage = yes }
						NOT = { faith = { has_doctrine = tenet_polyamory } }
						AND = {
							allowed_to_marry_same_sex_trigger = no
							scope:actor = { sex_same_as = scope:secondary_actor }
						}
					}
				}
				add_opinion = {
					target = scope:actor
					modifier = relative_in_blasphemous_union_opinion
				}
			}
		}	

		# Not up to them anymore...
		if = {
			limit = { has_trait = celibate }
			remove_trait = celibate
		}			
				
		# Spouses, if any exist, are also furious.
		if = {
			limit = {
				is_married = yes
				#A check to make sure you haven't somehow married them during the consideration time
				NOT = { any_spouse = { this = scope:actor } }
			}
			every_spouse = {
				hidden_effect = {
					if = {
						limit = {
							primary_spouse ?= {
								this = scope:secondary_actor
							}
							any_spouse = {
								count >= 2
							}
						}
						add_character_flag = {
							flag = flag_lowborn_exception
							days = 2
						}
						scope:secondary_actor = {
							new_primary_spouse_divorce_effect = {
								PREVIOUS_PS = scope:secondary_actor
								PARTNER = prev
							}
						}
						remove_character_flag = flag_lowborn_exception
					}
				}
				if = {
					limit = {
						NOT = { any_spouse = { this = scope:actor } }
					}
					add_opinion = {
						target = scope:actor
						modifier = forced_spouse_concubine_marriage_opinion
					}
				}
				divorce = scope:secondary_actor # no additional opinion hit, no scripted effect
				save_scope_value_as = { # For tooltips in the coming event
					name = was_spouse
					value = yes
				}
			}
		}
				
		# Stealing concubines pisses the former concubinist off.
		if = {
			limit = {
				is_concubine = yes
				#A check to make sure you haven't somehow gotten them as concubine during the consideration time
				NOT = { any_consort = { this = scope:actor } }
			}
			this.concubinist = {
				if = {
					limit = {
						NOT = {
							this = scope:recipient
						}
					}
					add_opinion = {
						target = scope:actor
						modifier = stole_concubine_opinion
					}
				}
				remove_concubine = scope:secondary_actor # no additional opinion hit, no scripted effect
			}
		}

		# Break any betrothals which may exist (without additional opinion penalties).
		if = {
			limit = {
				exists = betrothed
				#A check to make sure you haven't somehow gotten betrothed to them during the consideration time
				NOT = { betrothed = scope:actor }
			}
			break_betrothal = betrothed
		}

		# Having concubines means they're now no longer yours.
		if = {
			limit = { number_of_concubines > 0	}
			every_concubine = {
				scope:secondary_actor = {
					remove_concubine = prev
				}
			}
		}
		remove_character_flag = has_been_offered_as_concubine
		clear_demoted_consort_divorce_modifiers_effect = yes
		make_concubine_harem_politics_effect = {
			CHARACTER = scope:actor
			TARGET = scope:secondary_actor
		}
	}

	# Send us a notification about what we just did.
	scope:actor = {
		send_interface_toast = {
			type = event_toast_effect_neutral
			title = msg_concubine_offered_reversed_title
			right_icon = scope:recipient
			left_icon = scope:secondary_actor
			scope:actor = {
				make_concubine = scope:secondary_actor
				if = { #Scale opinion gain a bit depending on how cool the concubine is
					limit = {
						scope:secondary_actor = {
							OR = {
								has_trait = beauty_good_2
								has_trait = beauty_good_3
								num_of_good_genetic_traits > 1
								has_relation_lover = scope:actor
								has_relation_soulmate = scope:actor
							}
							trigger_if = {
								limit = {
									is_female = yes
								}
								age < define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
							}
						}
					}
					add_opinion = {
						target = scope:recipient
						modifier = grateful_opinion
						opinion = 40
					}
				}
				else_if = {
					limit = {
						scope:secondary_actor = {
							OR = {
								is_lowborn = no
								has_trait = beauty_good_1
								num_of_good_genetic_traits > 0
								has_relation_friend = scope:actor
								has_relation_best_friend = scope:actor
							}
							trigger_if = {
								limit = {
									is_female = yes
								}
								age < define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
							}
						}
					}
					add_opinion = {
						target = scope:recipient
						modifier = grateful_opinion
						opinion = 30
					}
				}
				else = {
					add_opinion = {
						target = scope:recipient
						modifier = grateful_opinion
						opinion = 20
					}
				}
			}
		}
	}
}

child_trait_manager_effect  = {	
	if = {
		limit = {
			has_any_bastard_trait_trigger = yes
		}
		remove_all_bastard_traits = yes
	}
	if = {
		limit = {
			has_trait = bastard_founder
		}
		remove_trait = bastard_founder
	}
}

child_promotion_effect = {
	# PARENT_1 is the "actor" (the partner/ruler)
	# PARENT_2 is the "recipient" (the concubine/spouse)
	
	$PARENT_2$ = {
		if = {
			limit = {
				any_child = {
					is_child_of = $PARENT_1$
					is_alive = yes
				}
			}
			every_child = {
				limit = {
					is_child_of = $PARENT_1$
					is_alive = yes
				}
				# First remove any bastard traits
				child_trait_manager_effect = yes
				if = {
					limit = {
						has_trait = child_of_concubine_female
					}
					remove_trait = child_of_concubine_female
				}
				else_if = {
					limit = {
						has_trait = child_of_concubine_male
					}
					remove_trait = child_of_concubine_male
				}
			}
		}
	}
}

child_demotion_effect = {
	# PARENT_1 is the "actor" (the partner/ruler)
	# PARENT_2 is the "recipient" (the concubine/spouse)
	
	$PARENT_2$ = {
		if = {
			limit = {
				any_child = {
					is_child_of = $PARENT_1$
					is_alive = yes
				}
			}
			every_child = {
				limit = {
					is_child_of = $PARENT_1$
					is_alive = yes
				}
				# First remove any bastard traits
				child_trait_manager_effect = yes
				
				# Then assign the appropriate concubine child trait
				if = {
					limit = {
						$PARENT_2$ = { is_female = yes }
					}
					add_trait = child_of_concubine_female
				}
				else = {
					add_trait = child_of_concubine_male
				}
			}
		}
	}
}

gain_influence_harem_politics_effect = {
	# RECIPIENT is the character receiving fluence
	# TARGET is offered consort
	# CHARACTER is the character receiving the TARGET
	$RECIPIENT$ = {
		if = {
			limit = {
				government_has_flag = government_has_influence
				exists = house
				exists = top_liege
				NOT = { has_character_flag = received_influence_offer_concubine_flag }
				$TARGET$ = {
					house ?= $RECIPIENT$.house
				}
				$CHARACTER$ = {
					government_has_flag = government_has_influence

					OR = { # Same realm check
						top_liege ?= $RECIPIENT$.top_liege
						this = $RECIPIENT$.top_liege
					}
					
					house ?= {
						NOT = { this = $RECIPIENT$.house }
						OR = {
							is_powerful_family = yes
							is_dominant_family = yes
							any_house_member = {
								this = $RECIPIENT$.top_liege
							}
						}
					}
				}
			}
			change_influence = {
				value = 20
				if = {
					limit = {
						$TARGET$ = {
							is_close_family_of = $RECIPIENT$
						}
					}
					if = {
						limit = {
							$CHARACTER$ = {
								is_independent_ruler = yes
								this = $RECIPIENT$.top_liege
							}
						}
						multiply = 2
					}
					else_if = {
						limit = {
							$CHARACTER$.house = {
								OR = {	
									is_dominant_family = yes
									any_house_member = {
										this = $RECIPIENT$.top_liege
									}
								}
							}
						}
						multiply = 1.5
					}
				}
			}
		}
	}
}

